如何解决NameError:名称'randomResponse'未定义

2024-10-03 02:42:38 发布

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

这是出现错误的代码:

print("Through 1-10 write a number that is going to represent how far you should throw the ball for " + playerCMD4 + "to catch") ; sleep(float(speed))
                    playerNumberCMD = raw_input()
                    import random
                    def allResponses(arg1):
                            allResponses = arg1
                    allResponses = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
                    def randomResponse(arg1):
                            randomResponse = arg1
                    randomResponse = random.choice(allResponses)
                    if randomResponce == playerNumberCMD:
                            print(playerCMD4 + " caught the ball.") ; sleep(float(speed))

我得到的错误是: 回溯(最近一次呼叫最后一次):

  File "classref3.py", line 3, in <module>
    class Dog:
  File "classref3.py", line 50, in Dog
    if randomResponce == playerNumberCMD:
NameError: name 'randomResponce' is not defined

Def不是正确的方式还是其他方式


Tags: thetois错误sleepfloatspeedprint
1条回答
网友
1楼 · 发布于 2024-10-03 02:42:38

如错误所述,您尚未定义变量“RandomResponse”

如果您查看上一行,您认为您已经定义了变量,那么您已经定义了“randomResponse”。注意不同的拼写。拼写必须相同

我还要提醒大家,不要在同一个脚本中对变量和函数使用相同的名称

相关问题 更多 >