Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:游戏菜单选项提示顺序错误_Python_Python 3.x - Fatal编程技术网

Python:游戏菜单选项提示顺序错误

Python:游戏菜单选项提示顺序错误,python,python-3.x,Python,Python 3.x,我目前正在创建我的第一个“大”python项目。在游戏中,你是一个必须使用异能与怪物战斗的巫师。如果你的生命值达到0,你就会失去生命。如果怪物的生命值达到0,你就赢了 我的问题是: 我已经创建了一个菜单,可以通过按键1、2、3和4进行操作。 代码如下: menu = int(input(''' Press 1 to battle! Press 2 to get to the shop Press 3 to get to

我目前正在创建我的第一个“大”python项目。在游戏中,你是一个必须使用异能与怪物战斗的巫师。如果你的生命值达到0,你就会失去生命。如果怪物的生命值达到0,你就赢了

我的问题是:
我已经创建了一个菜单,可以通过按键1、2、3和4进行操作。 代码如下:

menu = int(input('''
             Press 1 to battle!
             Press 2 to get to the shop
             Press 3 to get to the upgrades
             Press 4 to exit the game'''))
buying = int(input('''
               (1)Buy an apple!
               (2)Buy a cake!
               (3)Buy a MedPack!'''))

if menu == 1:
   print("You can use your Abilities with A, B and C")
   print(" ")
   while health_player >= 0:
       spells = input("Press a for an attack, b for a fire ball or C for a Thunder!"
       if spells == "A":
           damage_monster = random.randrange(10, 30)
           health_monster -= damage_monster
           print("The monster lost", damage_monster, "HP and still has", health_monster, "/200 left.")
           damage = random.randrange(10, 30)
           health_player -= damage
           print("You lost", damage, "HP and still have", health_player, "/200 left.")
           print(" ")
。。。然后是其他菜单选项:

elif menu == 2:
    print('''
    -----------------------
    Welcome to the shop!
    Buy an apple to get a part of your health back!
    ''')

elif menu == 3:
    pass

elif menu == 4:
    sys.exit()
但是,菜单选项选择未按预期工作。即使我按1、3或4,商店也会出现。我怎样才能解决这个问题

希望您能理解并帮助我。:)

提前谢谢

编辑: 根据新的信息和代码完全重新编写答案

如果我正确理解您的问题:
-如果我没有从菜单中选择“2”,为什么会提示我买东西?

这是因为“购买”
input
提示符紧跟在“菜单”
input
提示符之后。如下图所示。因为这个游戏是用脚本编写的,所以代码从上到下运行

menu = int(input('''
             Press 1 to battle!
             Press 2 to get to the shop
             Press 3 to get to the upgrades
             Press 4 to exit the game'''))
buying = int(input('''
               (1)Buy an apple!
               (2)Buy a cake!
               (3)Buy a MedPack!'''))
我建议您将“购买”提示移动到
elif菜单==2
块中

如图所示:

if menu == 1:
    # Do battle stuff ...
elif menu == 2:
    print('''
    -----------------------
    Welcome to the shop!
    Buy an apple to get a part of your health back!
    ''')
    # Move your 'buying' prompt here:
    buying = int(input('''
                   (1)Buy an apple!
                   (2)Buy a cake!
                   (3)Buy a MedPack!'''))
elif menu == 3:
    # Do menu 3 stuff ...
elif menu == 4:
    sys.exit()
我已经测试了这个逻辑,它似乎工作正常。试试看

建议: 1) 调查一下。这将给你一些权力和控制你的屏幕上的提示


2)考虑写你的游戏,或者你想把它向前一步,而不是作为一个脚本。这将为您提供更多的灵活性。试试看

一次问一个问题,好吧,我会读这篇文章,删除第一个问题。谢谢你,很抱歉你太苛刻了。我今天和昨天在这个项目上工作了相当长的一段时间,这是我有点担心的最大问题:/Thank Though你能发布准确的代码吗?这一行缺少一个右括号
spells=input(“按a表示攻击,按b表示火球,按C表示打雷!”
这是pastebin文档中的代码,请根据更新您的问题嘿:)感谢您的快速回复。我试过了,可惜没用。这是菜单代码menu=int(输入('Press 1 to battle,Press 2 to to shop,Press 3 to to the upgrades,Press 4 to exit the game'))buying=int(输入('1'买个苹果!(2)买个蛋糕!(3)买个药包!”)@Constantin-好的。今晚晚些时候我会去看看。同时,你介意用这个菜单代码更新你原来的问题吗?然后我可以测试整个块。这是pastebin文档pastebin.com/d9TH5BA9Wow中的代码谢谢!现在无法尝试,但我会尽快尝试。我在考虑给球员上一节课,在这节课上我会给他生命值,所以这是一般性的,但是我看了一下课,我就是不能完全理解。你能举一个班级的例子吗?我认为这会使代码更简短,更不用担心。上面的
classes
链接将带您访问w3schools网站,并以个人为例介绍课堂。所以你可以很容易地把这个逻辑发展成一个玩家。玩一玩,看看你过得怎么样。它肯定会为您的代码增加通用性和灵活性。这是值得理解的。(顺便说一句,如果这对您有效,请将答案视为已接受。谢谢!)