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()
我无法让程序正常循环,我不知道我做错了什么!如果我没有正确猜出这个数字,我应该可以按任意键再试一次,然后点击“q&#39;再次尝试,但当我按任何其他键时,它将无法正常循环。同样,我不知道这段代码有什么问题。
答案 0 :(得分:2)
您不会将pip
重置为零,因此您的程序就像用户已经进行了三次猜测一样。尝试这样的事情:
from faker import Factory
#----------------------------------------------------------------------
def create_values(fake):
""""""
print fake.boolean(chance_of_getting_true=50) # True
print fake.random_int(min=0, max=1) # 1
if __name__ == "__main__":
fake = Factory.create()
create_values(fake)
答案 1 :(得分:0)
当您检查密钥是否不等于q时,您必须重置猜测量。我把你的代码放到另一种方法中,只是更好的设计! 祝你好运!
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.")