简体   繁体   中英

Changing a variable in a def function

Okay, weird question. So I'm making a text adventure game for a school project, and I'm making a shop system. It's supposed to only allow you to buy one of each item, with the exception of potions.

schoice = str(input("> "))
def shopresults(item, itemvar, price):
  if schoice == str(item):
    if itemvar == 0 and gold >= price:
      print("Here ya go! One " + str(item) + " coming  right up!")
      # itemvar = 1 this should change the variable determining the status of the item, e.g. swordfire1 or swordfire2  
      gold == gold - price
      if item == "fire sword" or itemvar == "water sword" or itemvar == "thunder sword":
         freesword = 0
         elif itemvar == 1 and (item != potion1 or item != potion2 or item != potion3):
            print("You already have that, kiddo!")
         elif gold < price:
            print("You're a little short on gold there, bud...")
shopresults("fire sword", swordfire1, (100 - (100 * freesword)))
shopresults("flame sword", swordfire2, 500)

How do I change the variable that is called before you get into the shopresults program, in this case swordfire1, instead of the itemvar itself? swordfire1 determines if you have the 1st fire sword, and there's variables like this for every weapon. Ignore the freesword variable, you basically get one free sword in the game, and that's not the problem as far as I know. I know this is confusing, but please help if you can.

Variable assignment is done with = not == . And you need combine the conditions with parentheses:

schoice = str(input("> "))
def shopresults(item, itemvar, price):
  if schoice == str(item):
    if (itemvar == 0) and (gold >= price):
      print("Here ya go! One " + str(item) + " coming  right up!")
      # itemvar = 1 this should change the variable determining the status of the item, e.g. swordfire1 or swordfire2  
      gold = gold - price
      if (item == "fire sword") or (itemvar == "water sword") or (itemvar == "thunder sword"):
         freesword = 0
         elif (itemvar == 1) and (item != potion1) or (item != potion2) or (item != potion3):
            print("You already have that, kiddo!")
         elif gold < price:
            print("You're a little short on gold there, bud...")

shopresults("fire sword", swordfire1, (100 - (100 * freesword)))
shopresults("flame sword", swordfire2, 500)

I guess I'm wondering what is the value of swordfire1 and swordfire2 - they look like integers? My thinking is you should think about creating weapon objects and maintaining a registry of weapons. Based on the status, call your registry and return your weapon object to that variable. When I say registry you can use a simple dict or you can design a factory like pattern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM