为什么当我问是否要再做一次时,即使我说“不”,它为什么也会掷骰子?

时间:2018-07-04 01:14:43

标签: python function

我当时在写掷骰子游戏,但是当我问到“你想掷骰子吗?”的问题时,我说“不”,反正滚动。我猜想它与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()

1 个答案:

答案 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"))