简体   繁体   English

这个python程序怎么了?

[英]What's wrong with this python program?

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. 我正在尝试制作Derp Simulator小游戏,因为我很无聊,并且出现了一个问题,如果您尝试购买猫粮,它会将猫粮添加到库存中,但不会将其添加到猫粮数量中,并且不会带走钱。 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(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. 永远不会执行第二个return语句(return liqpota),因为您的函数在遇到的第一个返回处结束。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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