需要完成我的学校作业代码

时间:2016-05-29 15:43:09

标签: python variable-assignment

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,

3 个答案:

答案 0 :(得分:1)

当然,您必须将输入分配给playAgain,如下所示:

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

答案 1 :(得分:1)

您必须将输入分配给您在while条件下检查的变量。除此之外,您可以将提示作为input()的参数提供:

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

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

答案 2 :(得分: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)')