I'm teaching myself Python via Zed Shaw's LPTHW and he suggested to me that I try to condense my code so I can call up bits of it within a function. Right now this is boggling my mind and I can't figure out a way to condense it that doesn't result in an error regarding global and local variables in reference to weapons
. As it stands, this code works, but as you can see, it's long and gross.
### Shop / buy weapons room ###############
def buy_weapon(weapons):
global gold
"""big bit of code that allows you to buy a weapons from a weapon list.
The function acts a little differently after level zero weapons"""
global current_weapon
if weapons == level_zero_weapons:
sword_price = level_zero_price()
blunt_price = level_zero_price()
agile_price = level_zero_price()
print t.bright_magenta_on_black + """
Please type in the weapon you want to buy.
%s, price: %d gold pieces
%s, price: %d gold pieces
%s, price: %d gold pieces.
""" % (weapons[0], sword_price, weapons[1], blunt_price,weapons[2],
agile_price)
weapon_choice = raw_input(":> ")
if weapons[0] in weapon_choice and gold >= sword_price:
character_sheet.remove(character_sheet[9])
gold = gold - sword_price
character_sheet.insert(9, "You have %s gold pieces." % gold)
current_weapon = weapons[0]
inventory(weapons[0])
character_sheet.append("Current Weapon: %s" % current_weapon)
barracks()
elif weapons[1] in weapon_choice and gold >= blunt_price:
character_sheet.remove(character_sheet[9])
gold = gold - blunt_price
character_sheet.insert(9, "You have %s gold pieces." % gold)
current_weapon = weapons[1]
inventory(weapons[1])
character_sheet.append("Current Weapon: %s" % current_weapon)
barracks()
elif weapons[2] in weapon_choice and gold >= agile_price:
character_sheet.remove(character_sheet[9])
gold = gold - agile_price
character_sheet.insert(9, "You have %s gold pieces." % gold)
current_weapon = weapons[2]
inventory(weapons[2])
character_sheet.append("Current Weapon: %s" % current_weapon)
barracks()
else:
print "I dont know what %s means" % weapon_choice
next()
buy_weapon(level_zero_weapons)
elif weapons == level_one_weapons:
sword_price = level_one_price()
blunt_price = level_one_price()
agile_price = level_one_price()
print"""
Type in the weapon you want to buy, type quit to return to the barracks.
%s, price: %d gold pieces
%s, price: %d gold pieces
%s, price: %d gold pieces.
""" % (weapons[0], sword_price, weapons[1], blunt_price, weapons[2],
agile_price)
weapon_choice = raw_input(":> ")
if weapons[0] in weapon_choice and gold >= sword_price:
character_sheet.remove(character_sheet[9])
gold = gold - sword_price
character_sheet.insert(9, "You have %s gold pieces." % gold)
current_weapon = weapons[0]
inventory(weapons[0])
character_sheet.append("Current Weapon: %s" % current_weapon)
barracks()
elif weapons[1] in weapon_choice and gold >= blunt_price:
character_sheet.remove(character_sheet[9])
gold = gold - blunt_price
character_sheet.insert(9, "You have %s gold pieces." % gold)
current_weapon = weapons[1]
inventory(weapons[1])
character_sheet.append("Current Weapon: %s" % current_weapon)
barracks()
elif weapons[2] in weapon_choice and gold >= agile_price:
character_sheet.remove(character_sheet[9])
gold = gold - agile_price
character_sheet.insert(9, "You have %s gold pieces." % gold)
current_weapon = weapons[2]
inventory(weapons[2])
character_sheet.append("Current Weapon: %s" % current_weapon)
barracks()
elif "quit" in weapon_choice:
barracks()
else:
print "Either you dont have enough money, or I dont know what %s means" % weapon_choice
next()
buy_weapon(level_one_weapons)
elif weapons == level_two_weapons:
sword_price = level_two_price()
blunt_price = level_two_price()
agile_price = level_two_price()
print"""
Type in the weapon you want to buy, type quit to return to the barracks.
%s, price: %d gold pieces
%s, price: %d gold pieces
%s, price: %d gold pieces.
""" % (weapons[0], sword_price, weapons[1], blunt_price,weapons[2],
agile_price)
weapon_choice = raw_input(":> ")
if weapons[0] in weapon_choice and gold >= sword_price:
character_sheet.remove(character_sheet[9])
gold = gold - sword_price
character_sheet.insert(9, "You have %s gold pieces." % gold)
current_weapon = weapons[0]
inventory(weapons[0])
character_sheet.append("Current Weapon: %s" % current_weapon)
barracks()
elif weapons[1] in weapon_choice and gold >= blunt_price:
character_sheet.remove(character_sheet[9])
gold = gold - blunt_price
character_sheet.insert(9, "You have %s gold pieces." % gold)
current_weapon = weapons[1]
inventory(weapons[1])
character_sheet.append("Current Weapon: %s" % current_weapon)
barracks()
elif weapons[2] in weapon_choice and gold >= agile_price:
character_sheet.remove(character_sheet[9])
gold = gold - agile_price
character_sheet.insert(9, "You have %s gold pieces." % gold)
current_weapon = weapons[2]
inventory(weapons[2])
character_sheet.append("Current Weapon: %s" % current_weapon)
barracks()
elif "quit" in weapon_choice:
barracks()
else:
print "Either you dont have enough money, or I dont know what %s means" % weapon_choice
next()
buy_weapon(level_two_weapons)
elif weapons == level_three_weapons:
sword_price = level_three_price()
blunt_price = level_three_price()
agile_price = level_three_price()
print"""
Type in the weapon you want to buy, type quit to return to the barracks.
%s, price: %d gold pieces
%s, price: %d gold pieces
%s, price: %d gold pieces.
""" % (weapons[0], sword_price, weapons[1], blunt_price,weapons[2],
agile_price)
weapon_choice = raw_input(":> ")
if weapons[0] in weapon_choice and gold >= sword_price:
character_sheet.remove(character_sheet[9])
gold = gold - sword_price
character_sheet.insert(9, "You have %s gold pieces." % gold)
current_weapon = weapons[0]
inventory(weapons[0])
character_sheet.append("Current Weapon: %s" % current_weapon)
barracks()
elif weapons[1] in weapon_choice and gold >= blunt_price:
character_sheet.remove(character_sheet[9])
gold = gold - blunt_price
character_sheet.insert(9, "You have %s gold pieces." % gold)
current_weapon = weapons[1]
inventory(weapons[1])
character_sheet.append("Current Weapon: %s" % current_weapon)
barracks()
elif weapons[2] in weapon_choice and gold >= agile_price:
character_sheet.remove(character_sheet[9])
gold = gold - agile_price
character_sheet.insert(9, "You have %s gold pieces." % gold)
current_weapon = weapons[2]
inventory(weapons[2])
character_sheet.append("Current Weapon: %s" % current_weapon)
barracks()
elif "quit" in weapon_choice:
barracks()
else:
print "Either you dont have enough money, or I dont know what %s means" % weapon_choice
next()
buy_weapon(level_three_weapons)
elif weapons == level_four_weapons:
sword_price = level_four_price()
blunt_price = level_four_price()
agile_price = level_four_price()
print"""
Type in the weapon you want to buy, type quit to return to the barracks.
%s, price: %d gold pieces
%s, price: %d gold pieces
%s, price: %d gold pieces.
""" % (weapons[0], sword_price, weapons[1], blunt_price,weapons[2],
agile_price)
weapon_choice = raw_input(":> ")
if weapons[0] in weapon_choice and gold >= sword_price:
character_sheet.remove(character_sheet[9])
gold = gold - sword_price
character_sheet.insert(9, "You have %s gold pieces." % gold)
current_weapon = weapons[0]
inventory(weapons[0])
character_sheet.append("Current Weapon: %s" % current_weapon)
barracks()
elif weapons[1] in weapon_choice and gold >= blunt_price:
character_sheet.remove(character_sheet[9])
gold = gold - blunt_price
character_sheet.insert(9, "You have %s gold pieces." % gold)
current_weapon = weapons[1]
inventory(weapons[1])
character_sheet.append("Current Weapon: %s" % current_weapon)
barracks()
elif weapons[2] in weapon_choice and gold >= agile_price:
character_sheet.remove(character_sheet[9])
gold = gold - agile_price
character_sheet.insert(9, "You have %s gold pieces." % gold)
current_weapon = weapons[2]
inventory(weapons[2])
character_sheet.append("Current Weapon: %s" % current_weapon)
barracks()
elif "quit" in weapon_choice:
barracks()
else:
print "Either you dont have enough money, or I dont know what %s means" % weapon_choice
next()
buy_weapon(level_four_weapons)
else:
print"~~~There is a bug somwhere, forgot to assign (weapons)\n\n\n"
raw_input(t.white_on_red("""
Your current weapon is now a %s. Press Enter To Continue
""" % current_weapon))
global
s! If you want to keep it simple for now and don't go object oriented, you can use for example adict
to save your state, replacing the multiple global variables and pass that around as a function parameter. \$\endgroup\$