咖啡订单代码无法计算总价及统计销量问题求助
咖啡订单代码问题修复
问题原因分析
- 变量类型不匹配:
order()函数将用户输入转换为整数,但coff_order()中却用字符串(如'1')进行比较,导致所有价格计算条件都不触发,后续total()函数调用未定义的价格变量报错。 - 全局变量未声明:修改全局销量统计变量(如
tot_l_cap)时,未在coff_order()中声明global,导致Python将其视为局部变量,无法更新全局统计值。 - 订单计算逻辑错误:
total()直接累加所有咖啡价格变量,但用户通常只点一种咖啡,未点的咖啡价格变量未定义,引发报错;同时字符串与数字直接拼接会触发类型错误。 - 字符串处理遗漏赋值:
coff_summary()中coff_sum_ask.lower()未将结果存回原变量,导致输入大小写无法统一,判断逻辑失效。 - 函数未调用:定义了
coff_order()但未执行,导致价格计算和销量统计完全没运行。 - 缺少总销售额统计:每日销售汇总未计算当日总销售额。
修复后的完整代码
from colorama import Fore, Back, Style # 初始化每日销量统计 tot_l_cap = 0 tot_r_cap = 0 tot_l_esp = 0 tot_r_esp = 0 tot_l_lat = 0 tot_r_lat = 0 tot_l_iced_c_ic = 0 tot_r_iced_c_ic = 0 tot_l_iced = 0 tot_r_iced = 0 # 初始化当日总销售额 daily_total = 0.0 print(Fore.BLUE + 'Welcome to 4Shore Cafe!') print('We are happy to be of your assistance today.') print('Please select from our fabulous menu below: ') print(' ') print(' ') print('Please be advised to enter all coffee choices with the integer they are associated with :)') print(' ') print('-------------------------------------------------------------------------------------------------') print('1. Cappuccino: |Regular: $4.40 |Large: $4.95') print('-------------------------------------------------------------------------------------------------') print('2. Espresso: |Regular: $3.60 |Large: $4.15') print('-------------------------------------------------------------------------------------------------') print('3. Latte: |Regular: $3.85 |Large: $4.40') print('-------------------------------------------------------------------------------------------------') print('4. Iced Coffee With ice-cream and cream: |Regular: $4.40 |Large: $4.95') print('5. Iced Coffee Without: |Regular: $3.85 |Large: $4.40' ) print('-------------------------------------------------------------------------------------------------') def order(): global coff_type, coff_size, quant_coff coff_type = int(input("What coffee would you like to order? ")) coff_size = int(input('Enter 1 for regular, 2 for large: ')) quant_coff = int(input('How many would you like to order? ')) return coff_type, coff_size, quant_coff order() def coff_order(): global tot_l_cap, tot_r_cap, tot_l_esp, tot_r_esp, tot_l_lat, tot_r_lat global tot_l_iced_c_ic, tot_r_iced_c_ic, tot_l_iced, tot_r_iced, daily_total order_total = 0.0 if coff_type == 1: if coff_size == 2: order_total = 4.95 * quant_coff tot_l_cap += quant_coff elif coff_size == 1: order_total = 4.40 * quant_coff tot_r_cap += quant_coff elif coff_type == 2: if coff_size == 2: order_total = 4.15 * quant_coff tot_l_esp += quant_coff elif coff_size == 1: order_total = 3.60 * quant_coff tot_r_esp += quant_coff elif coff_type == 3: if coff_size == 2: order_total = 4.40 * quant_coff tot_l_lat += quant_coff elif coff_size == 1: order_total = 3.85 * quant_coff tot_r_lat += quant_coff elif coff_type == 4: if coff_size == 2: order_total = 4.95 * quant_coff tot_l_iced_c_ic += quant_coff elif coff_size == 1: order_total = 4.40 * quant_coff tot_r_iced_c_ic += quant_coff elif coff_type == 5: if coff_size == 2: order_total = 4.40 * quant_coff tot_l_iced += quant_coff elif coff_size == 1: order_total = 3.85 * quant_coff tot_r_iced += quant_coff # 更新当日总销售额 daily_total += order_total return order_total # 执行订单计算 current_order_total = coff_order() def total(order_total): subtotal = order_total gst = subtotal / 11 # GST按1/11计算 print('Your Order: ') print(f"The total cost of your order is ${subtotal:.2f}") print(f'GST included in total: ${gst:.2f}') total(current_order_total) def coff_summary(): coff_sum_ask = input('Would you like a summary of todays sales? ' ).lower() if coff_sum_ask == 'yes': print('TODAYS SALES:') print(f'Total Large Cappuccino Sales: {tot_l_cap}') print(f'Total Regular Cappuccino Sales: {tot_r_cap}') print(f'Total Large Espresso Sales: {tot_l_esp}') print(f'Total Regular Espresso Sales: {tot_r_esp}') print(f'Total Large Latte Sales: {tot_l_lat}') print(f'Total Regular Latte Sales: {tot_r_lat}') print(f"Total Large Iced Coffee's with Cream and Icecream Sales: {tot_l_iced_c_ic}") print(f"Total Regular Iced Coffee's with Cream and Icecream Sales: {tot_r_iced_c_ic}") print(f"Total Large Iced Coffee's Sales: {tot_l_iced}") print(f"Total Regular Iced Coffee's Sales: {tot_r_iced}") print(f"Total Daily Sales: ${daily_total:.2f}") elif coff_sum_ask == 'no': print('Okay! Have a Good Day!!') coff_summary()
关键修复点说明
- 类型匹配修正:将
coff_order()中的比较值从字符串改为整数,与order()的输入类型保持一致。 - 全局变量声明:在
coff_order()中声明所有需要修改的全局统计变量,确保能正确更新全局值。 - 订单计算优化:用单个
order_total变量存储当前订单金额,避免未定义变量问题;使用f-string格式化输出,解决字符串拼接类型错误。 - 字符串处理修复:将
coff_sum_ask.lower()的结果赋值回原变量,实现大小写不敏感判断。 - 补充函数调用:添加
coff_order()的调用,确保价格计算和销量统计执行。 - 新增总销售额统计:初始化
daily_total变量,每次订单后累加金额,在销售汇总中展示。
内容的提问来源于stack exchange,提问作者Charlee Candy
相关产品推荐
相关产品推荐

