所以我对编程很陌生,在谷歌的帮助下做了一个小游戏。游戏的第一个代码是有效的,但是我想问我如何添加一个函数来询问我是否想再玩一次,例如,如果我说“是”,那么它会再次玩游戏,并返回到“我在想一个数字…”或者如果我说“否”,那么它会退出
第二个代码是不起作用的代码,我尝试添加一个函数让游戏再次运行,并建议我添加另一个while循环,但不太确定放在哪里。目前第二个代码,当我猜到正确的数字,它只是关闭,不要求输入
我为这个愚蠢的问题道歉,但我想不通
import random
guessesTaken = 0
print("Hello, I am your computer. What is your name?")
myName = input()
number = random.randint(1, 20)
print("\nHello " + myName + ", and welcome to the game! Do you want to play?")
playGame = input()
while playGame == "Yes":
print("I am thinking of a number between 1 and 20, can you guess my number?\n")
while guessesTaken < 6:
print("Take a guess. ")
guess = input()
guess = int(guess)
guessesTaken += 1
if guess < number:
print("Your guess is too low.")
if guess > number:
print("Your guess is too high.")
if guess == number:
break
if guess == number:
guessesTaken = str(guessesTaken)
print("Well done " + myName + "! You guessed my number in " + guessesTaken + " guesses!")
if guess != number:
number = str(number)
print("Sorry " + myName + ", but you couldn't figure it out, my number was " + number)
break
while playGame == "No":
print("Then you opened this for nothing, goodbye")
break
input()
import random
guessesTaken = 0
print("Hello, I am your computer. What is your name?")
myName = input()
number = random.randint(1, 20)
print("\nHello " + myName + ", and welcome to the game! Do you want to play?")
playGame = input()
while playGame == "Yes" or playAgain == "Yes":
print("I am thinking of a number between 1 and 20, can you guess my number?\n")
while guessesTaken < 6:
print("Take a guess. ")
guess = input()
guess = int(guess)
guessesTaken += 1
if guess < number:
print("Your guess is too low.")
if guess > number:
print("Your guess is too high.")
if guess == number:
break
if guess == number:
guessesTaken = str(guessesTaken)
print("Well done " + myName + "! You guessed my number in " + guessesTaken + " guesses! \nWould you like to play again?")
playAgain = input()
if guess != number:
number = str(number)
print("Sorry " + myName + ", but you couldn't figure it out, my number was " + number + "\nWould you like to play again?")
playAgain = input()
break
while playAgain == "No":
print("Thank you for playing, goodbye")
break
while playGame == "No":
print("Then you opened this for nothing, goodbye")
break
input()
您可以将播放特性添加为函数,并在while循环中调用它
相关问题 更多 >
编程相关推荐