你们可以在Jupyter note中运行我的代码,请告诉我它有什么问题吗?我似乎不明白为什么它无法正常运行。每当我从库存中选择物品时,它都会给我带来结束的机会。我希望在龙打败时打印代码,并将打印语句打印到下一个房间。
import sys
print("""Hello! Type 1 to Continue""")
#ice sword - a
#fire sword - b
#electric sword - c
inv = {'a':'Ice Sword'}
#function to display the inventory
def display():
print("type 1 to display your inventory\n")
n = input()
if int(n)==1:
for i in inv:
print(i +" - "+ inv[i])
sequence = ['c','a','b']
for i in range(0,4):
#room-1 Fire dragon - ice sword win-firesword
if i==0:
print("You are in room-1 a FIRE DRAGON appears\n")
display()
print("choose your Item")
x = input()
if x == 'a' and i==0:
print("The fire dragon is defeated proceed to next room\n")
print("You have acquired a new Item 'Fire Sword'\n")
inv['b'] = 'Fire Sword'
else:
print("---GAME OVER---")
#room-2 Electric dragon - ice sword win-electric sword
if i==1:
print("You are in room-2 a ELECTRIC DRAGON appears\n")
display()
print("choose your Item")
x = input()
if x=='a' and i==1:
print("The electric dragon is defeated you proceed next room\n")
print("You have acquired a new Item 'Electric Sword'\n")
inv['c'] = 'Electric Sword'
else:
print("---GAME OVER---")
#room-3 Ice dragon - fire sword win-key
if i==2:
print("You are in room-3 a Ice dragon appears\n")
display()
print("choose your Item")
x = input()
if x=='b' and i==2:
print("The Ice dragon is defeated you __ next room\n")
print("You have acquired a new Item 'Key'\n")
inv['d'] = 'Key'
else:
print("---GAME OVER---")
#room-4 Dark beast Ganon specific order ([c a b])
if i==3:
print("Use the key you won to enter the last room\n")
display()
print("choose your Item")
x = input()
if x=='d' and i==3:
print("The Final boss DARK BEAST GANON appears\n")
else:
print("---GAME OVER---")
#used the key and the boss appears
print("to defeat this boss you need to use a specific combination of the swords\n")
print("Enter the sequence one by one and press enter after every value")
a = []
for i in range(0,3):
x = input()
a.append(x)
if a == sequence:
print("Congratulation you have completed the game")
#sys.exit()
else:
print("---GAME OVER---")
答案 0 :(得分:0)
这是一个构造错误的问题。我们不仅需要更多有关所发生一切事情的上下文,而且我还建议您特别指出出现问题的代码行,在这种情况下,还应指出“游戏结束”的代码行。
此外,无论是否在Jupyter中运行都无关紧要,只要您使用的Python版本相同,它就可以在任何运行位置以相同的方式工作。
答案 1 :(得分:0)
看起来您的错误是此循环的副作用:
for i in range(0,4):
if i==0: #room-1 Fire dragon - ice sword win-firesword
print("You are in room-1 a FIRE DRAGON appears\n")
display()
print("choose your Item")
x = input()
if x == 'a' and i==0:
请注意,即使仅在i==0
时打印,循环仍会通过i=1
,i=2
和i=3
执行。我突出显示的最后一行if x == 'a' and i==0:
的值为False,因为此时i==0
为False-i
为3
(因为循环已完成)。