2017-06-17 6 views
-3

Ich programmiere ein Spiel, um zu versuchen, meine Fähigkeiten in Python zu verbessern. In diesem Teil des Codes versuche ich ein Geschäft mit einem Geldsystem Testen einer Variablen gegen 5 verschiedene mögliche Antworten zu programmierenIch habe Probleme mit meinen elif Aussagen

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() 

Bei der Eingabe health potion der Code auf wie normalen geht aber mit den anderen Eingängen geht es zu dieser Teil des Codes

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

Mögliche Duplikat [Wie ich eine Variable gegen mehrere Werte testen?] (Https://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple- Werte) – jonrsharpe

+0

Diese Frage ist mehrere Variablen für mehrere Werte nicht eine Variable wie in meinem Code @jonrsharpe – Ricardo

+0

Ändern Sie Ihre dritte Zeile zu 'wenn Wahl in (" Gesundheitstrank "," Stärketrank "," Stärke Booster "," Rüstung Stück " "Nichts"): ' –

Antwort

-2

Ihr Problem ist mit dieser Aussage:

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

Vergleicht Strings wie dies funktioniert nicht. Sie müssen sicherstellen, dass es nicht in einem Array der Strings

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

Ansonsten ist, wird es immer wahr sein, so dass die erste Anweisung wird immer ausgeführt werden.

+0

Das ist nicht die Lösung. Sehen Sie meinen Kommentar oder die verknüpfte Frage von jonrsharpe. –

+0

Dies ist ein klares Duplikat - vielleicht sollten Sie überhaupt nicht antworten und stattdessen die Frage markieren. – Moira

0

Zum Spaß, hier ist eine deutlich erweiterte Version.

Mach dir keine Sorgen, wenn es nicht sofort einen Sinn ergibt; versuche, es durchzuspielen und herauszufinden, wie es funktioniert. Sobald Sie es vollständig verstanden haben, werden Sie Python besser verstehen!

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

Könntest du erklären, was jedes einzelne Teil des Codes ist und wie es funktioniert, weil ich sehr neu darin bin und nicht viel Erfahrung damit habe? @Hugh Bothwell – Ricardo