gpt4 book ai didi

Python 错误类型错误 : unsupported operand type(s) for +: 'function' and 'function'

转载 作者:太空宇宙 更新时间:2023-11-04 10:09:10 25 4
gpt4 key购买 nike

我知道网上有一些关于此错误的资料,但无论我尝试什么,似乎都无法解决问题。我昨天才开始学习 Python,绝对是初学者,所以请不要评判我的脚本。这只是一个简单的脚本,想要从餐厅获取顾客的订单并计算这顿饭的总价。这很俗气,但我会喜欢一些帮助。我无法让我的脚本来计算这顿饭的总费用,无论我尝试什么,它都是行不通的。你能告诉我我做错了什么以及如何根据客户的选择来计算这顿饭的总费用吗?此外,如果他们选择菜单上没有的项目,程序将关闭而不是让他们重试。为什么?非常感谢您的帮助。 :) 谢谢

这是我在终端中运行脚本时遇到的成本错误的图片。 Error1

这是我输入菜单上没有的内容时得到的图片。 Error2

这是我的脚本。

请注意,我添加了菜单中的项目只是为了向您展示它们是什么以及它们的成本。

    Apple = 3
Banana = 4
Kiwi = 2
Peach = 5
Hamburger = 12
Parma = 22
Steak = 24
Sandwhich = 10
Cream = 3
Cake = 8
Moose = 2
Soda = 3
Beer = 8
Wine = 12


def Fruit():
print("Welcome to The Buttler's Pantery")
Fruit = raw_input("what fruit would you like today?")

if (Fruit == "Apple"):
Main()
elif (Fruit == "Banana"):
Main()
elif (Fruit == "Kiwi"):
Main()
elif (Fruit == "Peach"):
Main()
else:
print("Sorry, but it appears that the item you have ordered is not on the menu")

def Main():
Main = raw_input("what Main would you like today?")

if (Main == "Hamburger"):
Dessert()
elif (Main == "Parma"):
Dessert()
elif (Main == "Steak"):
Dessert()
elif (Main == "Sandwhich"):
Dessert()
else:
print("Sorry, but it appears that the item you have ordered is not on the menu")


def Dessert():
Dessert = raw_input("what Dessert would you like today?")

if (Dessert == "Cream"):
Beverage()
elif (Dessert == "Cake"):
Beverage()
elif (Dessert == "Moose"):
Beverage()
else:
print("Sorry, but it appears that the item you have ordered is not on the menu")

def Beverage():
Beverage = raw_input("what Beverage would you like today?")

if (Beverage == "Soda"):
print(add(num1, num2, num3, num4))
elif (Beverage == "Beer"):
print(add(num1, num2, num3, num4))
elif (Beverage == "Wine"):
print(add(num1, num2, num3, num4))

else:
print("Sorry, but it appears that the item you have ordered is not on the menu")

def add(num1, num2, num3, num4):
return num1 + num2 + num3 + num4

def num1():
if (Fruit == "Apple"):
num1 = 3
elif (Fruit == "Banana"):
num1 = 4
elif (Fruit == "Kiwi"):
num1 = 2
elif (Fruit == "Peach"):
num1 = 5
else: num1 = 0

def num2():
if (Main == "Hamburger"):
num2 = 12
elif (Main == "Parma"):
num2 = 22
elif (Main == "Steak"):
num2 = 24
elif (Main == "Sandwhich"):
num2 = 10
else: num2 = 0

def num3():
if (Dessert == "Cream"):
num3 = 3
elif (Dessert == "Cake"):
num3 = 8
elif (Dessert == "Moose"):
num3 = 2
else: num3 = 0

def num4():
if (Beverage == "Soda"):
num4 = 3
elif (Beverage == "Beer"):
num4 = 8
elif (Beverage == "Wine"):
num4 = 12
else: num4 = 0









Fruit()

最佳答案

你正在用你的局部变量名重新定义你的函数名

难怪为什么会有混淆。

我知道 visual basic 用于返回值,但你不能在 python 中这样做。

只需重命名您的局部变量(分配给 raw_input 结果的变量就可以了

关于Python 错误类型错误 : unsupported operand type(s) for +: 'function' and 'function' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39314935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com