我遇到了我的elif语句问题

问题描述:

我正在编写一个游戏来尝试并提高我在Python中的技能。在这部分代码我想当你输入health potion代码继续这样正常的,但它转到其他输入编程与货币系统测试一个变量店对5个不同的可能的答案我遇到了我的elif语句问题

while True: 
       choice=str(input("What would you like to buy? (Type in 'nothing' when you don't want anymore items) ")) 
       if choice!="health potion" and "strength potion" and "strength booster" and "armour piece" and "nothing": 
        print() 
        next_line=input("I do not understand what you wrote. Try again please ") 
        print() 
       elif choice=="nothing": 
        next_line=input("The merchant says 'Thanks for business' ") 
        print() 
        break 
       elif choice=="health potion": 
        gold=gold-10 
        if gold<0: 
         gold=gold+10 
         next_line=input("Sorry but you don't have enough gold ") 
         print() 
        else: 
         next_line=input("You bought a health potion ") 
         health_potions=health_potions+1 
         next_line=input("You now have "+str(gold)+" gold coins ") 
         print() 
       elif choice=="strength potion": 
        gold=gold-15 
        if gold<0: 
         gold=gold+15 
         next_line=input("Sorry but you don't have enough gold ") 
         print() 
        else: 
         next_line=input("You bought a strength potion ") 
         strength_potions=strength_potions+1 
         next_line=input("You now have "+str(gold)+" gold coins ") 
         print() 
       elif choice=="strength booster": 
        gold=gold-45 
        if gold<0: 
         gold=gold+45 
         next_line=input("Sorry but you don't have enough gold ") 
         print() 
        else: 
         next_line=input("You boosted your strength ") 
         strength_booster=strength_booster+1 
         next_line=input("You now have "+str(gold)+" gold coins ") 
         print() 
       elif choice=="armour piece": 
        gold=gold-30 
        if gold<0: 
         gold=gold+30 
         next_line=input("Sorry but you don't have enough gold ") 
         print() 
        else: 
         next_line=input("You bought an armour piece ") 
         armour=armour+1 
         next_line=input("You now have "+str(gold)+" gold coins ") 
         print() 

代码

if choice!="health potion" and "strength potion" and "strength booster" and "armour piece" and "nothing": 
       print() 
       next_line=input("I do not understand what you wrote. Try again please ") 
       print() 
+4

的可能的复制[如何测试对多值一个变量?(https://*.com/questions/15112125/how-do-i-test-one-variable-against-multiple-值) – jonrsharpe

+0

这个问题是多个变量的多个值不是像我的代码中的一个变量@jonrsharpe – Ricardo

+0

改变你的第三行为“如果选择(健康药水”,“强度药水”,“强化助推器”,“护甲片” ,“nothing”):' –

您的问题,这部分是用这样的说法:

if choice!="health potion" and "strength potion" and "strength booster" and "armour piece" and "nothing": 

这样比较字符串不工作。您需要确保它不在字符串中

if choice not in ("health potion","strength potion","strength booster","armour piece","nothing"): 

否则它将始终为真,因此第一条语句将始终执行。

+0

这不是解决方案。看到我的评论或jonrsharpe的链接问题。 –

+0

这是一个明确的重复 - 也许你不应该回答,而是标记问题。 – Moira

为了好玩,这里有一个非常先进的版本。

不要担心,如果它没有立即意义;尝试追踪它并弄清它是如何工作的。一旦你完全理解它,你将对Python有更好的掌握!

class Character: 
    def __init__(self, name, health=50, strength=20, gold=200, inventory=None): 
     """ 
     Create a new character 

     inventory is a list of items (may have repeats) 
     """ 
     self.name = name 
     self.health = health 
     self.strength = strength 
     self.gold = gold 
     self.inventory = [] if inventory is None else list(inventory) 

    def buy(self, item): 
     """ 
     Buy an item 
     """ 
     if self.gold >= item.cost: 
      print(item.buy_response.format(name=item.name, cost=item.cost)) # print acceptance 
      self.gold -= item.cost            # pay gold 
      item.buy_action(self)            # apply purchased item to character 
      return True 
     else: 
      print("Sorry but you don't have enough gold.") 
      return False 

class Item: 
    def __init__(self, name, cost, buy_response="You bought a {name} for {cost} GP", buy_action=None): 
     # store values 
     self.name = name 
     self.cost = cost 
     # what to print on a successful purchase 
     self.buy_response = buy_response 
     # apply a purchased item to the character 
     self.buy_action = self.make_buy_action() if buy_action is None else buy_action 

    def make_buy_action(self): 
     def buy_action(char): 
      """ 
      Purchase default action: add item to character inventory 
      """ 
      char.inventory.append(self) 
     return buy_action 

    @staticmethod 
    def buy_strength_booster(char): 
     """ 
     Purchase strength booster action: increase character strength 
     """ 
     char.strength += 1 

    def __str__(self): 
     return self.name 

class Shop: 
    def __init__(self, name, *inventory): 
     """ 
     Create a shop 

     inventory is a list of (num, item); if num is None the store has an unlimited supply 
     """ 
     self.name = name 
     self.inventory = {item.name:(num, item) for num,item in inventory} 

    def visit(self, char): 
     """ 
     Serve a customer 
     """ 
     print("\nHowdy, {}, and welcome to {}!".format(char.name, self.name)) 

     while True: 
      print("\nWhat would you like to buy today? (type 'list' to see what's available or 'done' to leave)") 
      opt = input("{} GP> ".format(char.gold)).strip().lower() 
      if opt == 'done': 
       print("Have a great day, and c'mon back when you've got more gold!") 
       break 
      elif opt == 'list': 
       item_names = sorted(name for name, (num, item) in self.inventory.items() if num is None or num > 0) 
       if item_names: 
        print(", ".join(item_names)) 
       else: 
        print("Huh - looks like we're all sold out. Try again next week!") 
        break 
      elif opt in self.inventory: 
       num, item = self.inventory[opt] 
       if num is None or num > 0: 
        yn = input("That's {} GP. You want it? [Y/n]".format(item.cost)).strip().lower() 
        if yn in {'', 'y', 'yes'}: 
         if char.buy(item) and num is not None: 
          self.inventory[opt] = (num - 1, item) 
        else: 
         print("(scowling, the proprietor stuffs the {} back under the counter)".format(item.name)) 
       else: 
        print("'Fraid we're all out of those.") 
      else: 
       print("Sorry, hain't had one o' those around in a coon's age!") 

def main(): 
    # stock the store 
    shop = Shop("Dwarven Dave's Delving Deal Depot", 
     (6, Item("health potion", 10)), 
     (6, Item("strength potion", 15)), 
     (3, Item("strength booster", 45, "You boosted your strength!", Item.buy_strength_booster)), 
     (None, Item("armor piece",  30)) # unlimited stock 
    ) 

    # create a buyer 
    jeff = Character("Jeff") 

    # visit the store 
    shop.visit(jeff) 

if __name__ == "__main__": 
    main() 
+0

你能解释一下代码的每一部分是什么以及它是如何工作的,因为我对此很陌生,对它没有多少经验? @Hugh Bothwell – Ricardo