简体   繁体   中英

Why is this infinite while loop not working

def main():

    import random

    guesslist = []
    ErrorTol = 5

    guessesTaken = 0



    print("|--------------------------------------------------------------------|")
    print("|                                                                    |")
    print("|--------------------------------------------------------------------|")                                                           
    print("|     WELCOME! Please enter your name to begin this guessing game    |")
    print("|--------------------------------------------------------------------|")
    print("|                                                                    |")
    print("|--------------------------------------------------------------------|")
    myName = input()

    again = ""

    while again != "q":

        number = random.uniform(-300, 300)
        print('Well, ' + myName + ', I am thinking of a number between -300 and 300.')

        while guessesTaken < 3:
         print("Take a guess.") 
         guess = input()
         guess = int(guess)

         guesslist.append(guess) 

         guessesTaken = guessesTaken + 1

         if guess < number:
             print('Your guess is too low.')

         if guess > number:
             print('Your guess is too high.')

         if guess == number or (abs(number - guess) <= ErrorTol):
             break

        if guess == number or (abs(number - guess) <= ErrorTol):
         guessesTaken = guessesTaken
         print('Good job, ' + myName + '! You guessed my number in ' + str(guessesTaken) + ' guesses!')

         print("numbers you guessed:", guesslist)

        else:
         number = int(number)
         print('Nope. The number I was thinking of was ' + str(number))

        again = input("Hit 'q' to quit the program or any other keys to play the game again.").lower()[0]

main()

I can't get the program to loop properly, I have no clue what I did wrong! When I do not guess the number correctly, I should be able to hit any keys to try again and hit 'q' to try again, but when I hit any other keys it will not loop properly. Again, I have no clue what is wrong with this code.

You do not reset guessesTaken to zero so your program acts as if the user has already made there three guesses. Try something like this:

    again = input("Hit 'q' to quit the program or any other keys to play the game again.").lower()[0]
    guessesTaken = 0

You have to reset the amount of guesses when you check to see if the key is not equal to q. I put your code into another method, just better design! Good luck!

import random
def main():

print("|--------------------------------------------------------------------|")
print("|                                                                    |")
print("|--------------------------------------------------------------------|")                                                           
print("|     WELCOME! Please enter your name to begin this guessing game    |")
print("|--------------------------------------------------------------------|")
print("|                                                                    |")
print("|--------------------------------------------------------------------|")

guess()
def guess():

guesslist = []
ErrorTol = 5

guessesTaken = 0
myName = input()

again = ""

while again != "q":
    guessesTaken = 0

    number = random.uniform(-300, 300)
    print('Well, ' + myName + ', I am thinking of a number between -300 and 300.')

    while guessesTaken < 3:
     print("Take a guess.") 
     guess = input()
     guess = int(guess)

     guesslist.append(guess) 

     guessesTaken = guessesTaken + 1

     if guess < number:
         print('Your guess is too low.')

     if guess > number:
         print('Your guess is too high.')

     if guess == number or (abs(number - guess) <= ErrorTol):
         break

    if guess == number or (abs(number - guess) <= ErrorTol):
     guessesTaken = guessesTaken
     print('Good job, ' + myName + '! You guessed my number in ' + str(guessesTaken) + ' guesses!')

     print("numbers you guessed:", guesslist)

    else:
     number = int(number)
     print('Nope. The number I was thinking of was ' + str(number))

    again = input("Hit 'q' to quit the program or any other keys to play the game again.")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM