我正在努力制作一个Derp Simulator小游戏的东西,因为我很无聊,如果你试图购买catfood就会出现一个问题,它会将catfood添加到库存中,但不会添加到库存中猫粮数量并没有带走钱。我为商店使用的单独模块比实际游戏文件更多,所以如果有人可以尝试找出错误的地方,我会很感激。
我这样调用了商店功能:
shop(inv, balance, catfood, liqpota)
商店代码:
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()
主要代码:(库存件)
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
答案 0 :(得分:0)
第二个return语句(return liqpota)永远不会被执行,因为你的函数在遇到第一次返回时结束。您可以在单个语句中返回所有值,并在函数调用中解压缩它们。我在下面列举了一个例子。
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)