【问题标题】:Text based dungeon game python基于文本的地牢游戏python
【发布时间】:2018-08-10 02:19:56
【问题描述】:

你们能在 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---")

【问题讨论】:

    标签: python anaconda jupyter


    【解决方案1】:

    这是一个结构糟糕的问题。我们不仅需要更多关于正在发生的事情的上下文,而且我还建议您特别指出问题发生的代码行,在这种情况下,“游戏结束”的代码也是如此。

    此外,它是否在 Jupyter 中运行并不重要,只要您使用的 Python 版本相同,它就会以相同的方式运行。

    【讨论】:

      【解决方案2】:

      看起来你的错误是这个循环的副作用:

      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=1i=2i=3 执行。我突出显示的最后一行 if x == 'a' and i==0: 的计算结果为 False,因为此时 i==0 为 False - i3(因为循环已完成)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-06-13
        • 1970-01-01
        • 1970-01-01
        • 2020-09-19
        • 2021-06-25
        • 1970-01-01
        • 2013-07-08
        相关资源
        最近更新 更多