Skip to main content
 首页 » 编程设计

python之方法未定义

2025年05月04日15JustinYoung

我是编程新手,正在尝试用 Python 编写不同版本的 DiceRoller 游戏。

我的代码如下,我得到一个doAgain is not defined错误。

我不确定这是像缩进这样简单的事情,还是我需要在某处休息一下。

我知道这可能是重复的,但我仍然很难找到这个确切的问题。

import random 
min = 1 
max = 6 
 
roll_again = 'yes' 
 
while roll_again == 'yes' or roll_again == 'y': 
    print 'Rolling the dice...' 
    print 'The values are...' 
    print random.randint(min,max) 
    print random.randint(min,max) 
    doAgain() 
 
def doAgain(): 
    userInput = raw_input('\nWould you like to roll the dice again? \nYes \nNo') 
 
    if userInput == 'Yes': 
        roll_again 
    elif userInput == 'No': 
        print ('Thank you for playing!') 
    else: 
        print ('You have entered an incorrect response.') 

请您参考如下方法:

调用前需要先定义doAgain()。将 def doAgain(): 移动到 while 循环之上。