我当时在写掷骰子游戏,但是当我问到“你想掷骰子吗?”的问题时,我说“不”,反正滚动。我猜想它与answer
变量有关。但是,我希望它打印“也许下次”。你能帮助我吗?这是我的代码:
import random
def response():
if answer == "yes" or answer == "Yes" :
rolldice()
else:
print("Maybe next time!")
def rolldice():
randomnumb = random.randrange(1,7)
print("You got number " + str(randomnumb) + "!")
answer = input("Would you like to roll the dice again? \n ")
response()
answer = input("Would you like to roll the dice? \n")
response()
答案 0 :(得分:2)
尝试一下。您必须将答案作为参数传递给函数响应:
import random
def response(answer):
if answer == "yes" or answer == "Yes" :
rolldice()
else:
print("Maybe next time!")
def rolldice():
randomnumb = random.randrange(1,7)
print("You got number " + str(randomnumb) + "!")
response(input("Would you like to roll the dice again? \n "))
response(input("Would you like to roll the dice? \n"))