gpt4 book ai didi

python - 当我猜不透时,我试图重玩游戏(当我猜不透或找到正确答案时,再玩一次)

转载 作者:行者123 更新时间:2023-12-04 07:43:21 25 4
gpt4 key购买 nike

我的目标是在我玩完游戏后能够再次运行代码
自动地。在代码的底部,我尝试编写一个可以重新运行游戏的代码,但是它似乎有一个语法错误

import random 

MAX_GUESSES = 5 #max number of guesses allowed

MAX_RANGE = 20 #highest possible number

#show introductionpygame
print("welcome to my franchise guess number game")
print("guess any number between 1 and", MAX_RANGE)
print("you will have a range from", MAX_GUESSES, "guesses")

#choose random target
target = random.randrange(1, MAX_RANGE + 1)

#guess counter
guessCounter = 0

#loop fovever
while True:
userGuess = input("take a guess:")
userGuess =int(userGuess)

#increment guess counter
guessCounter = guessCounter + 1

#if user's guess is correct, congratulate user, we're done
if userGuess == target:
print("you got it la")
print("it only took you",guessCounter, "guess(es)")
break
elif userGuess < target:
print("try again, your guess is too low.")
else:
print(" your guess was too high")

#if reached max guesses, tell answer correct answer, were done
if guessCounter == MAX_GUESSES:

print(" you didnt get it in ", MAX_GUESSES, "guesses")
print("the number was", target)
break

print("Thanks for playing ")


#main code
while True:
playOneRound() #call a function to play one round of the game
goAgain = input("play again?(press ENTER to continue, or q to quit ):")
if goAgain == "q":
break

最佳答案

你忘了提到函数名 playOneRound .下面的代码工作正常。

import random

MAX_GUESSES = 5 # max number of guesses allowed

MAX_RANGE = 20 # highest possible number

# show introductionpygame
print("welcome to my franchise guess number game")
print("guess any number between 1 and", MAX_RANGE)
print("you will have a range from", MAX_GUESSES, "guesses")

def playOneRound():
# choose random target
target = random.randrange(1, MAX_RANGE + 1)

# guess counter
guessCounter = 0

# loop fovever
while True:
userGuess = input("take a guess:")
userGuess = int(userGuess)

# increment guess counter
guessCounter = guessCounter + 1

# if user's guess is correct, congratulate user, we're done
if userGuess == target:
print("you got it la")
print("it only took you", guessCounter, "guess(es)")
break
elif userGuess < target:
print("try again, your guess is too low.")
else:
print(" your guess was too high")

# if reached max guesses, tell answer correct answer, were done
if guessCounter == MAX_GUESSES:
print(" you didnt get it in ", MAX_GUESSES, "guesses")
print("the number was", target)
break

print("Thanks for playing ")

# main code

while True:
playOneRound() # call a function to play one round of the game
goAgain = input("play again?(press ENTER to continue, or q to quit ):")
if goAgain == "q":
break

关于python - 当我猜不透时,我试图重玩游戏(当我猜不透或找到正确答案时,再玩一次),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67327176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com