2016-05-29 151 views
-3
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 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 treasure!') 
    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 = 2 
    1 
    input() 

在這裏你可以看到正在玩的遊戲與人。最後,當你需要回答是或否來再次玩時,再次玩遊戲和退出系統的適當編碼應該是什麼?我有一個類似的問題,我必須編輯另一個遊戲,再次播放或不再播放功能。需要完成我的作業代碼

感謝B,

回答

1

當然,你必須指定輸入playAgain這樣的:

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

謝謝,但你如何觸發no功能? – njr0502

+0

如果用戶回答「否」(或者是「yes」),程序將退出,這是我所期望的行爲。 – domoarrigato

1

你必須輸入分配給您的while條件檢查變量。除此之外,您可以提供提示作爲參數傳遞給input()

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

    playAgain = input('Do you want to play again? (yes or no)\n') 
+0

如何觸發no功能 – njr0502

+0

'while'循環只會在'playAgain'不再是'yes'或'y'時退出。 – schwobaseggl

0

還你實際上並不需要chooseCave功能。

playAgain = 'yes' 
while playAgain == 'yes' or playAgain == 'y': 
    displayIntro() 
    checkCave(input('Which cave will you go into? (1 or 2)')) 
    playAgain=input('Do you want to play again? (yes or no)') 
+0

沒有價值呢? – njr0502