Python代码不执行代码段

时间:2017-04-17 17:45:18

标签: python

我正在使用Invent with Python中的一个程序来实现我自己的故事。由于某种原因,从chooseVillage开始再到play部分的代码不起作用。它只是没有显示。你能帮我么?提前谢谢。

代码如下所示

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 treasure 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 spoopy...')
    time.sleep(2)
    print('A large dragon jumps out in front of you! He looks at you and...')
    print()
    time.sleep(2)

    friendlyCave = random.randint(1,2)

    if chosenCave == str(friendlyCave):
        print('Gives you his treasure')
        print('You decide to spend it at a village')
    else:
        print('Gobbles you down in one bite.')
        quit()

def chooseVillage():
  village = '' 
  while village != '1' and village != '2' and village != '3':
    print('Which village will you go to? (1, 2, or 3)')
    village = input()

def checkVillage(chosenVillage):
  print('You approch the city')
  time.sleep(2)
  print('It is filled with many stores,')
  time.sleep(2)
  print('In one store, a man is standing at it...')
  time.sleep(2)
  print('He has a gun, and has his hand by it')
  time.sleep(2)
  print('He says Welcome to my store,')
  print()
  time.sleep(2)

  friendlyVillage = random.randint(1, 2, 3)

  if chosenVillage == str(friendlyVillage):
    print('And he sells you some weapons.')
  else:
    print('He looks at you and says:')
    time.sleep(2)
    print('You are not from around here,')
    time.sleep(4)
    print('And he shoots you! Better luck next time')
    quit()


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

在repl.it here链接到此计划。

1 个答案:

答案 0 :(得分:0)

函数chooseVillage在任何时候都没有被执行。如果您通过播放,然后再说no进行播放,则可以从命令行直接执行chooseVillage()来运行执行此操作的代码。我想你要修改它的是,在caveNumber = chooseCave()之前添加print('Do you want to play cave or village?')然后使用输入来运行chooseCave()chooseVillage()

如果您搜索chooseVillage()的代码,您会看到它仅被实例化一次,即函数为def chooseVillage():且后来未执行。