【问题标题】:python text adventure validate user inputpython文本冒险验证用户输入
【发布时间】:2015-03-08 14:54:11
【问题描述】:

只是一个简单的问题。我真的很喜欢我的文字冒险游戏。当用户输入多个单词时,如何验证用户输入?我希望他们能够写 fire at chandelier 或类似的东西,但我一次只能验证一个词。

print("input fire chandelier")
user = input()

if user in ("fire", "chandelier"):
    print("works")
else:
    print("fails")

现在我的拍摄代码是这样的。它只验证一个词。

user  = input()

if user == "cover" or user == "stand" or user == "reload" or user == "fire"
or user == "wait" or user == "chandelier":
    while turn >= 1:
#player shoots if fire entered shoot is greater than 1
            #and player is standing 
            elif user == "fire" and shoot >= 1 and standing == True and chandelier < 2:

                #does hit random number between 1 and 2 1 hits 2 misses
                doesHit = random.randint(1, 2)
                if doesHit == 1:
                    print("you shoot one round hitting Martin but his skin is made of metal the bullet bounces off")
                    #-1 ammo
                    shoot = shoot - 1
                    #check if in cover
                    cover = False
                    #counts down turn
                    turn = turn - 1
                    break
                elif doesHit > 1:
                    print("you shoot but it misses")
                    turn = turn - 1
                    shoot = shoot - 1
                    break
            #shoot the chandelier to damage martin
            elif user in ["fire", "chandelier"] and shoot >= 1 and standing == True:
                doesHit = random.randint(1, 2)
                if doesHit == 1:
                    print("You shoot one round hitting the chandelier it hangs by one chain")
                    #-1 ammo
                    shoot = shoot - 1
                    #check if in cover
                    cover = False
                    #counts down turn
                    turn = turn - 1
                    chandelier = chandelier + 1
                    break
                elif doesHit > 1:
                    print("you shoot but it misses")
                    turn = turn - 1
                    shoot = shoot - 1
                    break

            #can shoot martin now chandelier has fallen
            elif user == "fire" and shoot >= 1 and standing == True and chandelier >= 2:

                #does hit random number between 1 and 2 1 hits 2 misses
                doesHit = random.randint(1, 2)
                if doesHit == 1:
                    print("you shoot one round hitting Martin")
                    #-1 ammo
                    shoot = shoot - 1
                    #check if in cover
                    cover = False
                    #counts down turn
                    turn = turn - 1
                    break
                elif doesHit > 1:
                    print("you shoot but it misses")
                    turn = turn - 1
                    shoot = shoot - 1
                    break

【问题讨论】:

  • 你能更好地解释你想要做什么吗?现在您正在接受用户输入,您将其称为user,然后测试它是单词'fire' 还是单词'chandelier'。您可以通过在元组中添加 'fire chandelier''fire at chandelier' 来获得您实际询问的工作内容,但这看起来也不是您想要做的。
  • 始终使用 raw_input() 而不是 input()(如果使用 Python 2.x)。看到这个:stackoverflow.com/a/7710959/1268926
  • @kedar 不在 Python 3 中。
  • 我希望它验证两种不同的情况,玩家可以一回合射击,但他们也可以射击枝形吊灯。我想验证他们是选择拍摄枝形吊灯还是只是正常拍摄

标签: python python-3.x text adventure


【解决方案1】:

试试这个:

print("input fire chandelier")
user = input()

if "fire" in user or "chandelier" in user:
     print ("works")
else:
     print("fails")

【讨论】:

  • 这是我认为 OP 打算对原始代码执行的操作,但随着命令列表的增加,这将变得非常复杂。
  • 谢谢这是我一直在寻找的东西,我会投票,但我没有足够的代表:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-25
  • 2015-10-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多