为什么当它问我是否想再做一次时,我说“不”,它却在掷骰子?

2024-10-05 10:22:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一个掷骰子游戏,但当我得到一个问题“你想再次掷骰子吗?”我说“不”,无论如何它都会滚。我猜这与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()

Tags: thetoansweryouinputresponsedefrandom
1条回答
网友
1楼 · 发布于 2024-10-05 10:22:43

试试这个。必须将answer作为参数传递给函数response:

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

相关问题 更多 >

    热门问题