与elif的错误。说elif是语法错误

时间:2014-08-10 23:26:47

标签: python if-statement

diamondCave = random.randint(1, 3)
    goldCave = random.randint(1, 3)
    while diamondCave == goldCave:
        goldCave = random.randint(1, 3)

    if chosenCave == str(diamondCave):
    pygame.mixer.init()
pygame.mixer.music.load("test.wav")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy() == True:
    continue
    print('full of diamonds!')
elif: chosenCave == str(goldCave):
    print('full of gold!')
else:
        print('hungry and 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 = input()
else:
        pygame.mixer.init()
pygame.mixer.music.load("test.wav")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy() == True:
    continue

我想制作代码,以便每次挑选胸部时,它都会发出声音。当钻石被拾取时,它会产生一种很酷的声音,当拾取金币时,会播放不同的声音,当胸部吃掉你时,它应该让游戏超过声音。每次我使用声音代码时,都会说elif语句是无效的语法。我做错了什么?

2 个答案:

答案 0 :(得分:3)

问题是elif:是语法错误。

elif的重点是它意味着else if。就像你无法在没有条件的情况下写if:一样,你无法在没有条件的情况下写elif:

同时,:之后的语句显然应该是条件表达式,而不是语句。所以你几乎肯定想要这个:

elif chosenCave == str(goldCave):

答案 1 :(得分:2)

删除":"在elif之后

elif: chosenCave == str(goldCave):

像这样:

elif chosenCave == str(goldCave):