這是整個程序。我已經寫過問題出現在我的屏幕上。您可以從here拿到正本(第6章,龍之王國)如何解決「無效語法」錯誤?
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 goblin')
print('is greedy and wants to loot 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('The goblin sees you and tells you too hand over your stuff')
time.sleep(2)
print("oh! I forgot you are an apprentice looking Merlin you can use ur magic on him")
time.sleep(1)
print("You can choose betweeen, fire, water, air or earth")
print()
time.sleep(2)
friendlyCave = random.randint(1, 2)
if chosenCave == str(friendlyCave):
print('Gives you his treasure!')
else:
print('Takes your stuff and run')
def spell(magic):
這是什麼地方,我認爲出現問題。
if magic == 'air' or magic == 'fire' or magic == 'water' or magic == 'earth' #This is where the problem occurs I think it is because of some prior codes
def choosespell(maguc)#choose between spells
if magic == 'air':
print('you blew the goblin away')
elif magic == 'fire':
print('you burned the goblin to death')
elif magic == 'water':
print('the goblin drowned to death')
elif magic == 'earth':
print('The veins chocked him to death')
magic = input()
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()
錯誤的縮進。你知道領先空間在Python中扮演着至關重要的角色嗎? – Matthias
你*認爲*這是問題發生的地方?你有沒有檢查錯誤信息中提到的行號? – Junuxx
第二個代碼塊包含很多錯誤,因此很難知道您是否在問題中插入文本時做得不好,或者如果這確實是您擁有的代碼。請確保問題中的代碼與編輯器中的代碼相同。正如Matthias所說;在python中縮進非常重要! – Markus