2017-04-17 119 views
0

我正在使用一個來自Invent的Python程序,龍的領域之一,來創造我自己的故事。出於某種原因,從chooseVillage開始再次播放的代碼不起作用。它只是不顯示。你能幫我麼?提前致謝。Python代碼不能執行代碼

的代碼是如repl.it here如下所示

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

鏈接到此程序。

回答

0

函數chooseVillage沒有在任何點執行。如果您播放它,然後說no再次播放,您可以從命令行直接執行chooseVillage()運行代碼。我猜你想要做的修改是,在caveNumber = chooseCave()之前,加入print('Do you want to play cave or village?'),然後使用輸入有要麼運行chooseCave()chooseVillage()

如果您完成chooseVillage()代碼搜索,你會看到它是唯一實例化什麼一次,這是功能是def chooseVillage():,而不是隨後執行。