运行 dragon.py 后没有错误消息



我目前正在开始"用Python发明你自己的电脑游戏"一书,出于某种奇怪的原因,尽管多次检查并发现了不止一些失误(拼写错误变量,在需要":"时使用";"等(,但我键入的dragon.py拒绝在IDLE中运行。

更糟糕的是,我没有收到错误消息;它只是显示"RESTART: /Users/yosemite/Documents/dragon.py",我又回到了提示符下。 但是,在此处找到的该网站的正式版本运行良好。

有人知道我在这里做错了什么吗? 更新:这是我的代码,以前忘了包含它:

import random
import time
def displayIntro():
    print ('You are in a land full of dragons.  In front of you.')
    print ('you see two caves.  In one cave, the dragon is friendly.')
    print ('and will share his reasure with you.  The other dragon')
    print ('is greedy and hungry, and will eat you on sight.')
    print()
def chooseCave():
    cave = ''
    while cave != '1' and cave !='2':
        print ('Which cave will you go into?  ( 1 or 2 )')
        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, 2)
    if chosenCave == str(friendlyCave):
        print('Gives you his treasures!')
    else:
        print('Goobles you up in onebite!')
    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()

您发布的代码一旦正确缩进,就会导入两个模块,定义三个函数,然后结束。 在程序结束时显示提示是 IDLE 应该做的。

在最后添加以下函数调用会使执行继续。

checkCave(chooseCave(((

如果您从 (mac?( 控制台运行程序

python -i /Users/yosemite/Documents/dragon.py

您将看到相同的行为,即>>>提示的外观。

相关内容

  • 没有找到相关文章

最新更新