You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python开发:如何为字符串菜品分配价格并计算订单总价

三明治店菜单项目的价格计算问题

我正在开发一个三明治店菜单的小型项目,已完成大部分功能,但需要为每个菜单菜品分配整数价格,并将所选菜品的价格相加计算最终订单总价。

现有代码如下:

import pyinputplus as pyip

bread = ['White', 'Wheat', 'Sourdough'] #The Menu Options
protein = ['Chicken', 'Turkey', 'Ham', 'Tofu']
cheese = ['Cheddar', 'Swiss', 'Mozzarella']
sauce = ['Mayo', 'Mustard', 'Lettuce', 'Tomato']

OP1 = pyip.inputMenu(bread, prompt = 'What kind of bread? \n') #Asking for Bread
OP2 = pyip.inputMenu(protein, prompt = 'What kind of Protein? \n') #Asking for Protien
def OP3(): #Asking for Cheese and what kind they would like
    cyn = pyip.inputYesNo('Want any cheese? ')
    if cyn == 'yes':
        pyip.inputMenu(cheese, prompt = 'What kind of Cheese? \n')
def OP4(): #Asking for Sauce and what kind they would like
    syn = pyip.inputYesNo('Want any Sauce? ')
    if syn == 'yes':
        pyip.inputMenu(sauce, prompt = 'What kind of Sauce? \n')

OP3() #Calling the Cheese and Sauce Functions
OP4()

OP5 = pyip.inputNum(prompt='How Many Sandwiches would you Like? ') #Asking how many sandwiches they would like.

d = {'White': 3, 'Wheat': 1, 'Sourdough': 4, 'Chicken': 3, 'Turkey': 5, 'Ham': 3, 'Tofu': 9, 'Cheddar': 3, 'Swiss': 2, 'Mozzarella': 4, 'Mayo': 3, 'Mustard': 3, 'Lettuce': 3, 'Tomato': 90} #Dictionary I thought would convert the strings into numbers

print((OP1 + OP2 + OP3 + OP4) * OP5) #Final Price Equation

运行时出现类型错误:can only concatenate str (not "function") to str,请问如何正确为用户输入的字符串类型菜品分配整数价格,并实现价格的累加计算?


解决方案

问题根源

  1. OP3、OP4是函数而非选中的菜品值:直接把函数名拿来做字符串拼接,会触发类型错误,因为函数和字符串不能直接相加。
  2. 函数未返回选中的菜品:OP3和OP4只处理了用户输入,但没有把用户选择的芝士/酱料返回出来,导致无法获取对应的价格。
  3. 错误的价格计算方式:原代码试图拼接字符串(OP1+OP2),而不是从字典中取出对应的价格数值相加。

修改后的代码

import pyinputplus as pyip

# 菜单选项
bread = ['White', 'Wheat', 'Sourdough']
protein = ['Chicken', 'Turkey', 'Ham', 'Tofu']
cheese = ['Cheddar', 'Swiss', 'Mozzarella']
sauce = ['Mayo', 'Mustard', 'Lettuce', 'Tomato']

# 菜品价格映射字典
price_dict = {
    'White': 3, 'Wheat': 1, 'Sourdough': 4,
    'Chicken': 3, 'Turkey': 5, 'Ham': 3, 'Tofu': 9,
    'Cheddar': 3, 'Swiss': 2, 'Mozzarella': 4,
    'Mayo': 3, 'Mustard': 3, 'Lettuce': 3, 'Tomato': 90
}

# 获取用户选择的面包和蛋白质
selected_bread = pyip.inputMenu(bread, prompt='What kind of bread? \n')
selected_protein = pyip.inputMenu(protein, prompt='What kind of Protein? \n')

# 获取用户选择的芝士(可选)
def get_cheese():
    want_cheese = pyip.inputYesNo('Want any cheese? ')
    if want_cheese == 'yes':
        return pyip.inputMenu(cheese, prompt='What kind of Cheese? \n')
    return None  # 不选则返回None

# 获取用户选择的酱料(可选)
def get_sauce():
    want_sauce = pyip.inputYesNo('Want any Sauce? ')
    if want_sauce == 'yes':
        return pyip.inputMenu(sauce, prompt='What kind of Sauce? \n')
    return None

selected_cheese = get_cheese()
selected_sauce = get_sauce()

# 获取购买数量
quantity = pyip.inputNum(prompt='How Many Sandwiches would you Like? ')

# 计算单个三明治的价格
single_price = price_dict[selected_bread] + price_dict[selected_protein]
if selected_cheese:
    single_price += price_dict[selected_cheese]
if selected_sauce:
    single_price += price_dict[selected_sauce]

# 计算总价并输出
total_price = single_price * quantity
print(f"Total price: ${total_price}")

关键修改点

  • 让函数返回选中的菜品:get_cheese()和get_sauce()函数在用户选择后返回对应的菜品名称,不选则返回None。
  • 基于字典获取价格:从price_dict中取出每个选中菜品对应的整数价格,再进行数值相加。
  • 分步骤计算价格:先算单个三明治的价格,再乘以购买数量得到总价,逻辑更清晰。
  • 变量名更具可读性:把OP1/OP2这类模糊的变量名改成selected_bread、selected_protein,代码更容易维护。

内容的提问来源于stack exchange,提问作者twalk126

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 23:17:39