所以我在制作一个猜谜游戏函数时遇到了一些麻烦,这个函数可以记录你正确的次数。我现在有两个问题:
这是我的密码,一团糟:
def GuessingGame(score):
rng = random.Random()
numbertoguess = rng.randrange(1,10)
guess = int(input ("What is your guess for a number between 1 and 10?"))
if numbertoguess == guess:
print ("Great guess! You're correct.")
int (score = score + 1)
return score
else:
print ("Wrong, the number was "+str(numbertoguess)+".")
playagain = "yes"
score = 0
while playagain == "yes":
print ("The score is now "+str(GuessingGame(score))+".")
score = GuessingGame(score)
playagain = input ("Play again? (yes or no)")
if playagain != "yes":
print ("Goodbye")
else:
pass
此行实际上是在调用函数:
您只需使用:
score
是一个变量,可以这样使用要回答第二个问题,您不是在
return
子句中。你知道吗而不是这样:
在这两种情况下,您都可以返回分数,如下所示:
此外,此行可能不是您想要的:
没有理由cast这样,只要用这行字:
或:
最后一个注意事项是
GuessingGame
的样式最好是:根据PEP8
相关问题 更多 >
编程相关推荐