基于文本的地下城游戏python
你们能在Jupyter note中运行我的代码并告诉我它有什么问题吗?我似乎不明白为什么它不能正常工作。每次我从库存中选择商品时,它都会让我的游戏结束。我希望无论巨龙是否被击败,都能打印代码,并将打印语句发送到下一个房间基于文本的地下城游戏python,python,anaconda,jupyter,Python,Anaconda,Jupyter,你们能在Jupyter note中运行我的代码并告诉我它有什么问题吗?我似乎不明白为什么它不能正常工作。每次我从库存中选择商品时,它都会让我的游戏结束。我希望无论巨龙是否被击败,都能打印代码,并将打印语句发送到下一个房间 import sys print("""Hello! Type 1 to Continue""") #ice sword - a #fire sword - b #electric sword - c inv = {'a':'Ice Sword'} #functio
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---")
这是一个结构糟糕的问题。我们不仅需要更多的上下文来描述正在发生的一切,而且我还建议您特别指出问题发生的代码行,在本例中,代码也用于“游戏结束”的位置
此外,无论它是否在Jupyter中运行,只要您使用的Python版本相同,它都将以相同的方式运行 看来您的bug是此循环的副作用:
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
执行。我突出显示的最后一行,如果x='a'和I==0:
,则计算结果为False,因为此时I==0
为False-I
为3
(因为循环已完成)