Donc, je suis nouveau sur python et j'ai ce défi: je dois faire le jeu Guess the Number, entre moi et l'ordinateur. C'est donc là que je suis jusqu'à présent.

import random


the_number = random.randint(1, 4)
guess = 0

print(the_number)
while guess != the_number:
    guess = int(input("Please enter a number: "))
    if guess > the_number:
        print("Player guess lower...\n")
    elif guess < the_number:
        print("Player guess higher...\n")
    else:
        print("Game Over! The number was", the_number,"The Player wins!")
        break


    guess = random.randint(1, 100)

    if guess > the_number:
        print("Computer guess lower...\n")
    elif guess < the_number:
        print("Computer guess higher...\n")
    else:
        print("Game Over! The number was", the_number,"The Computer wins!")
    break


print("Thank you for playing")

Je veux savoir comment faire pour que ça ne s'arrête pas tant que l'un de nous n'a pas raison?

0
Flo Chelsea 30 mai 2020 à 08:17

3 réponses

Votre erreur à la ligne 27 break . Déplacez-le sous else: ou supprimez-le . En fait, à la ligne 27 break n'est pas nécessaire . Ainsi, le code final sera comme ceci:

import random


the_number = random.randint(1, 4)
guess = 0

print(the_number)
while guess != the_number:
    guess = int(input("Please enter a number: "))
    if guess > the_number:
        print("Player guess lower...\n")
    elif guess < the_number:
        print("Player guess higher...\n")
    else:
        print("Game Over! The number was", the_number,"The Player wins!")
        break


    guess = random.randint(1, 100)

    if guess > the_number:
        print("Computer guess lower...\n")
    elif guess < the_number:
        print("Computer guess higher...\n")
    else:
        print("Game Over! The number was", the_number,"The Computer wins!")


print("Thank you for playing")
0
Osadhi Virochana Jayasinghe Si 30 mai 2020 à 05:59

Vous pouvez faire quelque chose comme ça pour rendre l'ordinateur plus intelligent.

import random

the_number = random.randint(1, 4)
guess = 0

print(the_number)
minPossible = 0
maxPossible = 100

while guess != the_number:
    guess = int(input("Please enter a number: "))
    if guess > the_number:
        print("Player, guess lower...\n")
        if guess < maxPossible:
            maxPossible = guess - 1
    elif guess < the_number:
        print("Player, guess higher...\n")
        if guess > minPossible:
            minPossible = guess + 1
    else:
        print("Game Over! The number was", the_number, "The Player wins!")
        break

    guess = random.randint(minPossible, maxPossible)

    if guess > the_number:
        print("Computer, guess lower...\n")
        maxPossible = guess - 1
    elif guess < the_number:
        print("Computer, guess higher...\n")
        minPossible = guess + 1
    else:
        print("Game Over! The number was", the_number,"The Computer wins!")


print("Thank you for playing")
0
zenalc 30 mai 2020 à 05:55

Votre problème est l'instruction break à la fin de la boucle while. Le code effectue une itération dans la boucle une fois, puis le break termine la boucle.

0
Brenden Price 30 mai 2020 à 05:21
62098156