TypeError:不支持的操作数类型 - :' function'和'功能'

时间:2015-12-22 22:29:53

标签: python function

我刚刚开始使用python进行编程,我制作了这个小程序,总结了你买过的n件的总价。 然后它总结了你已售出的东西的总价并返回你的东西 赚来的。所以我似乎对功能有些混淆,但我无法找到错误,有人可以帮助我吗? (对不起我的英语不好) 这是输出:

Type 'earn' to see if you earned something
Type 'quit' to exit
Type here: earn
BOUGHT
Things you've bought: 3
SOLD
Things you've sold: 3
EARN
Traceback (most recent call last):
File "/Users/daze/Documents/earn.py", line 52, in <module>
main()
File "/Users/daze/Documents/earn.py", line 11, in main
earn()
File "/Users/daze/Documents/earn.py", line 50, in earn
return sell_sum - buy_sum
TypeError: unsupported operand type(s) for -: 'function' and 'function'

这是完整的代码:

def main():
print("Type 'earn' to see if you earned something")
print("Type 'quit' to exit")
choice_menu = input("Type here: ")
if choice_menu == 'earn':
    print("BOUGHT")
    buy_sum()
    print("SOLD")
    sell_sum()
    print("EARN")
    earn()
    yes_or_no()
elif choice_menu == 'quit':
    print("Quitting..")
else:
    print("Wrong key!")
    main()
def yes_or_no():
    choice_continue = None
    while choice_continue != 'y' or choice_continue != 'n':
        choice_continue = input("Do you want to continue?(y/n)")
        print("Insert a valid key!")
    if choice_continue == 'y':
        main()
    else:
        print("Quitting..")
def buy_sum():
    n_buy = eval(input("Things you've bought: "))
    buy_sum = 0
    cont = 1
    price_buy = 0
    while cont <= n_buy and price_buy < 0:
        price_buy = eval(input("Price ", str(cont), ": "))
        buy_sum = buy_sum + price_buy
        print("Actual shipment:", buy_sum)
        cont = cont + 1
    return buy_sum
def sell_sum():
    n_sell = eval(input("Things you've sold: "))
    sell_sum = 0
    cont = 1
    price_sell = 0
    while cont <= n_sell and price_sell < 0:
        price_sell = eval(input("Price ", str(cont), ": "))
        sell_sum = sell_sum + price_sell
        print("Actual sold:", buy_sum)
        cont = cont + 1
    return sell_sum
def earn():
    return sell_sum - buy_sum

main()

告诉我为什么它给了我这个错误以及我犯了哪个错误,如果在某些事情或其他事情上有错误,比如不要纠正我,请让我注意到。 谢谢。

1 个答案:

答案 0 :(得分:0)

您需要调用最后一行中的函数来获得结果

def earn():
    return sell_sum() - buy_sum()

另外,在main函数的末尾调用main()是个坏主意。我打赌你永远不会遇到这个代码的问题,但它只是不断地将堆栈帧添加到堆栈中,最终会溢出。