这是我提供的代码而不是3代码,所以这只是我的头脑。代码中可能存在很多错误,因为我刚刚在两天前开始。当我运行时,钻石选择工作正常,但金和饥饿的胸部混杂在一起。
import random
import time
import pygame
def displayIntro():
print ('You are in a land full of chests. In front of you,')
print ('you see three caves. In one cave, the chest is full of diamonds.')
print ('in the second cave, the chest is full of iron')
print ('the last chest is greedy and hungry, and will eat you on sight.')
print ()
def chooseCave():
cave = ''
while cave != '1'and cave !='2'and cave != '3':
print ('Which cave will you go into (1,2 or 3)')
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('the chest is....')
print()
time.sleep(2)
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:
print('full of diamonds!')
elif print('full of gold!'):
pass
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
答案 0 :(得分:4)
看看这一节,特别是elif
案例:
if chosenCave == str(diamondCave):
print('full of diamonds!')
elif print('full of gold!'):
pass
else:
print('hungry and gobbles you down in one bite!')
此处print
是elif
的条件,即执行它,检查其结果值None
,然后继续else
的情况,因为None
的布尔值为False
。
请改为尝试:
if chosenCave == str(diamondCave):
print('full of diamonds!')
elif chosenCave == str(goldCave):
print('full of gold!')
else:
print('hungry and gobbles you down in one bite!')