I am trying to make a Derp Simulator little game thing 'cuz I'm bored, and an issue popped up where if you try to buy catfood it adds catfood to the inventory but does not add it to the catfood count AND it doesn't take money away. I'm using a separate module for the shop than the actual game file so if anyone could try and find what's wrong I would appreciate it.
I called the shop function like this: shop(inv, balance, catfood, liqpota)
Shop Code:
from functions import *
def shop(inv, balance, catfood, liqpota):
while True:
print "Welcome to the shop."
print "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"
print "( To purchase an item; enter the letter of the item. )"
print "( If you want to exit; enter 'back'. )"
print "A] $5 Cat Food - 'Not Just For Cats'"
print "B] $7 Liquified Potatoes - 'Who Would Want These?'"
print
com = raw_input("Purchase: ")
divider()
if com == "back" or com == "Back":
break
elif com == "a" or com == "A":
if "Cat Food" in inv:
if balance < 7:
print "You have insufficient funds."
elif balance > 7 or balance == 7:
catfood = catfood + 1
balance = balance - 7
print "Purcahse succcessful."
return catfood
return liqpota
if not "Cat Food" in inv:
if balance < 7:
print "You have insufficient funds."
elif balance > 7 or balance == 7:
catfood = catfood + 1
balance = balance - 7
inv.append("Cat Food")
print "Purchase successful."
return catfood
return liqpota
elif com == "b" or com == "B":
print "WIP"
break
else:
print "Invalid Item/Command."
divider()
Main Code: (Inventory Part)
elif com == "inventory" or com == "Inventory":
tmp_invnum = 1
print "Cat Food = " + str(catfood)
print "Liquified Potatoes = " + str(liqpota)
print
for invf in inv:
print str(tmp_invnum) + "] " + invf
tmp_invnum += 1
The second return statement (return liqpota) is never executed because your function ends at the first return encountered. You can return all the values in a single statement and unpack them at the function call. I've included an example below.
def divider():
print '*' * 80
def shop(inv, balance, catfood, liqpota):
while True:
print "Welcome to the shop."
print "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"
print "( To purchase an item; enter the letter of the item. )"
print "( If you want to exit; enter 'back'. )"
print "A] $5 Cat Food - 'Not Just For Cats'"
print "B] $7 Liquified Potatoes - 'Who Would Want These?'"
print
com = raw_input("Purchase: ")
divider()
if com == "back" or com == "Back":
break
elif com == "a" or com == "A":
if "Cat Food" in inv:
if balance < 7:
print "You have insufficient funds."
elif balance > 7 or balance == 7:
catfood = catfood + 1
balance = balance - 7
print "Purcahse succcessful."
# return all four variables at once
return (catfood,liqpota,inv,balance)
if not "Cat Food" in inv:
if balance < 7:
print "You have insufficient funds."
elif balance > 7 or balance == 7:
catfood = catfood + 1
balance = balance - 7
inv.append("Cat Food")
print "Purchase successful."
# return all four variables at once
return (catfood,liqpota,inv,balance)
elif com == "b" or com == "B":
print "WIP"
break
else:
print "Invalid Item/Command."
divider()
return (catfood,liqpota,inv,balance)
catfood = 0
liquified_potatoes = 0
balance = 30
inv = []
print 'catfood %s potatoes %s inventory %s balance %s' % (catfood,liquified_potatoes,inv,balance)
catfood, liquified_potatoes, inv, balance = shop(inv, balance, catfood, liquified_potatoes)
print 'catfood %s potatoes %s inventory %s balance %s' % (catfood,liquified_potatoes,inv,balance)
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.