简体   繁体   English

为什么这个无限的while循环不起作用

[英]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. 当我无法正确猜出数字时,我应该可以按任意键重试,然后按“ q”键重试,但是当我按任何其他键时,它将无法正确循环。 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. 您无需将guessesTaken重置为零,因此您的程序就好像用户已经在这里进行了三个猜测一样。 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. 当您检查密钥是否不等于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.")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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