龙游戏不起作用,说定义变量是未定义的

时间:2013-04-27 16:00:51

标签: python python-3.3

我正在制作一个游戏,您可以从三个洞穴中进行选择,每个洞穴中都有一条龙。当我运行这个程序时,它拒绝运行,并说potatocave是未定义的。我正在使用python 3.我需要知道它为什么不接受定义的potatocave,我做错了什么,以及是否有更简单的方法。

编辑:我再次运行它,它说selectedCave是未定义的。 回溯错误说:

Traceback (most recent call last):
  File "C:\Python33\Projects\Dragon.py", line 32, in <module>
    if chosenCave == str(friendlyCave):
NameError: name 'chosenCave' is not defined
import random
import time
time.sleep (3)
def displayIntro():
    print('You are in a land full of dragons. In front of you,')
    print('you see three caves. In one cave, the dragon is friendly')
    print('and will share his treasure with you. Another dragon')
    print('is greedy and hungry, and will eat you on sight.')
    print('The last dragon is a Potarian and gives free potatoes.')

def chooseCave():
    cave = ''
    while cave != '1' and cave != '2' and cave != '3':
        print('Which cave will you go into? (1, 2 or 3)')
        cave = input()

    return cave

def checkCave(chosenCave):
    print('You approach the cave...')
    time.sleep(2)
    print('It is dark and spooky...')
    time.sleep(2)
    print('A large dragon jumps out in front of you! He opens his jaws and...')
    print()
    time.sleep(2)

friendlyCave = random.randint(1, 3)
potatocave = random.randint(1, 3)
while potatocave == friendlyCave:
    potatocave = random.randint(1, 3)
if chosenCave == str(friendlyCave):
    print('Gives you his treasure!')
elif chosenCave == str(potatocave):
    print ('Millions of potatoes rain from the sky.')
else:
    print('Gobbles you down in one bite!')

playAgain = 'yes'
while playAgain == 'yes' or playAgain == 'y':

    displayIntro()

    caveNumber = chooseCave()

    checkCave(caveNumber)

    print('Do you want to play again? (yes or no)')
    playAgain = input()

P.S这不是最终版本,我只是用一个土豆洞作为占位符来找出三个洞穴的概念而不是我原来的两个洞穴。

1 个答案:

答案 0 :(得分:4)

好吧,错误信息说明了这一点:当您尝试将其与chosenCave(即第32行)进行比较时,您尚未定义str(friendlyCave)


想象一下你是解释者并从头开始学习你的脚本。你的行动方针是:

  1. import randomimport timesleep for 3 seconds; 因此,randomtime现在是已识别的名称。

  2. 定义函数displayIntrochooseCavecheckCave;那些现在也是指各自职能的已知名称。

  3. 分配friendlyCavepotatocave。重新分配后者。

  4. chosenCavestr(friendlyCave)进行比较...等待,chosenCave是什么?


  5. 但是,正如DSM所说,如果第28-37行作为checkCave函数体的一部分缩进,那么一切都会正常工作:

    def checkCave(chosenCave):
        print('You approach the cave...')
        time.sleep(2)
        print('It is dark and spooky...')
        time.sleep(2)
        print('A large dragon jumps out in front of you! He opens his jaws and...')
        print()
        time.sleep(2)
    
        friendlyCave = random.randint(1, 3)
        potatocave = random.randint(1, 3)
        while potatocave == friendlyCave:
            potatocave = random.randint(1, 3)
        if chosenCave == str(friendlyCave):
            print('Gives you his treasure!')
        elif chosenCave == str(potatocave):
            print ('Millions of potatoes rain from the sky.')
        else:
            print('Gobbles you down in one bite!')