Python购物车程序:调用ordering()后无法触发inside_cart()?
问题排查与修复方案
咱们先扒拉一下问题根源:
核心问题1:inside_cart()的循环逻辑直接"自杀"了
你看inside_cart()里的while循环最后加了个无条件的break——不管用户选Y还是N,第一次执行完循环体就直接跳出while了,所以哪怕你调用完ordering(),程序也不会再次回到inside_cart()的循环里,直接结束了。
核心问题2:ordering()里的first变量没有全局声明(隐性坑)
虽然你在全局定义了first = True,但ordering()里用while first==True的时候,没有用global first声明,Python会把它当成局部变量处理,虽然当前代码里你没修改first,但这会埋下隐患,万一之后要修改first的值直接报错。
次要问题:try_again的处理不一致
选汉堡(代码1)的时候,try_again没转小写,用户输入大写Y的话会直接跳出,和其他菜品的逻辑不一致,得统一一下。
修复后的完整代码
print("(1)Home") print("- - - - - Welcome to BSITEA - - - - -") print("\n") food =["Burger","Fries","Hotdog","Sundae","Coke Float"] price =[15,10,20,30,50] cart =[] cost =[] counter = 0 first = True def menu(): print("- - - - - Menu - - - - -\n") collection = [["\nCode"," Description"," Size"," Price"], [" 1"," Burger"," Medium"," 15"], [" 2"," Fries"," Medium"," 10"], [" 3"," Hotdog"," Medium"," 20"], [" 4"," Sundae"," Medium"," 30"], [" 5"," Coke Float"," Medium"," 50"]] for a in collection: for b in a: print(b,end=' ') print() menu() def ordering(): global counter, first # 声明全局变量 print("\nPlease select the CODE of the food you want to order") while first==True: order = input("Type the code you want to order: \n") #burger if order == ("1"): print("You have selected \n") print(food[0]) cart.append(food[0]) cost.append(price[0]) counter= counter + 15 try_again = input("\nWould you like to add more? (Y/N) ").lower() # 统一转小写 if try_again != "y": break #fries elif order == ("2"): print("You have selected \n") print(food[1]) cart.append(food[1]) cost.append(price[1]) counter= counter + 10 try_again = input("\nWould you like to add more? (Y/N) ").lower() if try_again != ("y"): break #hotdog elif order == ("3"): print("You have selected \n") print(food[2]) cart.append(food[2]) cost.append(price[2]) counter= counter + 20 try_again = input("\nWould you like to add more? (Y/N) ").lower() if try_again != ("y"): break #sundae elif order == ("4"): print("You have selected \n") print(food[3]) cart.append(food[3]) cost.append(price[3]) counter= counter + 30 try_again = input("\nWould you like to add more? (Y/N) ").lower() if try_again != ("y"): break #coke float elif order == ("5"): print("You have selected \n") print(food[4]) cart.append(food[4]) cost.append(price[4]) counter= counter + 50 try_again = input("\nWould you like to add more? (Y/N) ").lower() if try_again != ("y"): break else: print("\nInvalid input. Please put only the CODE of the item\n") def inside_cart(): global first while first == True: print(' ') print(' ') print("The inside of your cart are currently") print(cart) print(cost) print(counter, " Pesos") suggestion = input("would you like to order more? Y/N: ").lower() if suggestion == "y": ordering() else: print("Thank you for ordering!") first = False # 修改first来退出循环,结束程序 ordering() inside_cart()
修复点说明
- 移除了
inside_cart()里的无条件break:改成当用户选N时,把first设为False,这样while循环会自然退出,程序结束;如果选Y,调用ordering()后,回到inside_cart()的循环,继续显示购物车。 - 给
ordering()和inside_cart()加上了global声明:明确使用全局变量,避免局部变量混淆。 - 统一了
try_again的处理逻辑:所有分支都把输入转成小写,不管用户输入Y还是y都能正确识别。 - 修正了菜单里的"Borgar"拼写错误(强迫症犯了😂)
现在测试一下:选Y继续下单,完成后会回到inside_cart()显示购物车,循环直到你选N退出,完全符合预期效果。
内容的提问来源于stack exchange,提问作者morkie
相关产品推荐
相关产品推荐

