我一直在使用我的python 3,我制作了一个简单的项目,这是一个游戏"猜数字",我在这里遇到问题,我可以'明白。直到我在脚本中添加一些东西来检查guess = input()
是否为数字时才发现游戏。而且我搞砸了,当我运行它时,它总是不断向我发送错误消息。
你们可以查看我的代码吗?如果可以检查guess = input()
是否为数字。
Zelandini
import random
def main():
USER = input('Hello there wht is your name?')
print('Hello, ' + USER + '')
question = input('Do you want to play a game? [Yes/No]')
if question == 'n' or question == 'No' or question == 'no' or question == 'N' or question == 'NO':
print('Bye, ' + USER + '!')
if question == 'y' or question == 'Yes' or question == 'Y' or question == 'yes':
print("Okay, the game is to guess my number,")
replay()
def replay():
tries = 1
number = random.randint(1, 10)
play_again = True
print("I'm thinking a number between 1 and 10")
Guess = input("Have a guess?")
while play_again:
if Guess > number:
print("Go lower, your number is too big")
if Guess < number:
print("Go higher, you number is to small")
while Guess != number:
tries += 1
Guess = int(input("Try again?"))
if tries == 4:
print('You have out of tries')
print("\nWould you like to play again?")
response = input("> ").lower()
if response not in ("yes", "y"):
play_again = False
print("Bye!")
replay()
if Guess > number:
print("Go lower, your number is too big")
if Guess < number:
print("Go higher, you number is to small")
if Guess == number:
print("Congratulations! You've got it")
print("\nWould you like to play again?")
response = input("> ").lower()
if response not in ("yes", "y"):
play_again = False
print("Bye you Sad!")
replay()
if __name__ == "__replay__":
replay()
if __name__ == "__main__":
main()
答案 0 :(得分:0)
如果您运行此略微调整的代码( INDENTATIONS&amp; SCOPES ),您将不再收到不间断的错误消息,并可专注于正确使用该功能。我不会为你破坏,快乐的调试。 :)
import random
def main():
USER = input('Hello there wht is your name?')
print('Hello, ' + USER + '')
question = input('Do you want to play a game? [Yes/No]')
if question == 'n' or question == 'No' or question == 'no' or question == 'N' or question == 'NO':
print('Bye, ' + USER + '!')
if question == 'y' or question == 'Yes' or question == 'Y' or question == 'yes':
print("Okay, the game is to guess my number,")
replay()
def replay():
tries = 1
number = random.randint(1, 10)
play_again = True
while play_again:
print("I'm thinking a number between 1 and 10")
guess = input("Have a guess?")
while guess != number:
tries += 1
guess = int(input("Try again?"))
if tries == 4:
print('You have out of tries')
print("\nWould you like to play again?")
response = input("> ").lower()
if response not in ("yes", "y"):
play_again = False
print("Bye!")
replay()
if guess > number:
print("Go lower, your number is too big")
if guess < number:
print("Go higher, you number is to small")
if guess == number:
print("Congratulations! You've got it")
print("\nWould you like to play again?")
response = input("> ").lower()
if response not in ("yes", "y"):
play_again = False
print("Bye you Sad!")
replay()
if name == "main": main()