你如何做到这一点,以便稍后可以在不使用任何数组、列表、集合等的情况下打印出循环中的先前输入?

问题描述 投票:0回答:1

我正在创建一个简单的食品订购系统,它不能使用循环和数组、列表、集合等,而且我无法确定为什么程序会为每个订单显示相同的食品。该程序可以接受 1-3 位顾客的订单,他们每人点一份主菜、配菜和饮料。

strt = int(input("1: Order\n2: Exit\n"))
if strt == 1:
  print("\nMENU:")
  print("Mains:\n      ID       Type        Price")
  print(
    "      1  Chicken           90.00\n      2  Pork              105.00\n      3  Fish              120.00\n      4  Beef              135.00"
  )
  print("Sides:\n      ID      Type         Price")
  print(
    "      1  Steamed Rice      20.00\n      2  Shredded Corn     35.00\n      3  Mashed Potatoes   50.00\n      4  Steam Vegetables  65.00"
  )
  print("Drinks:\n      ID      Type         Price")
  print(
    "      1  Mineral Water     25.00\n      2  Iced Tea          35.00\n      3  Soda              45.00\n      4  Fruit Juice       55.00"
  )
  print(
    "Enter the ID of the item you want to order. Enter 0 if you don't want an item for that category.\n"
  )

  cust = int(input("How many people are in your group? "))
  ordr = 0
  msoc = 0
  pwno = 0
  mprc = float(0)
  sprc = float(0)
  dprc = float(0)
  mtot = float(0)
  stot = float(0)
  dtot = float(0)

  while ordr != cust and cust <= 3:
    ordr = ordr + 1
    print(f"\nOrder {ordr}:")

    if ordr <= cust or msoc == n:
      main = int(input("  Main: "))
      if main == 1:
        mprc = float(90)
        print("       Chicken")
      elif main == 2:
        mprc = float(105)
        print("       Pork")
      elif main == 3:
        mprc = float(120)
        print("       Fish")
      elif main == 4:
        mprc = float(135)
        print("       Beef")
      elif main == 0:
        print("       None")

      side = int(input("  Side: "))
      if side == 1:
        sprc = float(20)
        print("       Steamed Rice")
      elif side == 2:
        sprc = float(35)
        print("       Shredded Corn")
      elif side == 3:
        sprc = float(50)
        print("       Mashed Potatoes")
      elif side == 4:
        sprc = float(65)
        print("       Steamed Vegetables")
      elif side == 0:
        print("       None")

      drnk = int(input("  Drink: "))
      if drnk == 1:
        dprc = float(25)
        print("       Mineral Water")
      elif drnk == 2:
        dprc = float(35)
        print("       Iced Tea")
      elif drnk == 3:
        dprc = float(45)
        print("       Soda")
      elif drnk == 4:
        dprc = float(55)
        print("       Fruit Juice")
      elif drnk == 0:
        dprc = float(0)
        print("       None")

    msoc = input("Is this order correct (y/n)? ")
    if msoc == "y":
      pwno = input("Proceed with the next order (y/n)? ")
      if pwno == "y":
        continue
      elif pwno == "n":
        ordr = cust
        continue
    elif msoc == "n":
      ordr = ordr - 1
      continue
  cncl = input("Cancel all orders (y/n)? ")
  if cncl == "y":
    print("")
  elif cncl == "n":
    xcld = input("Exclude an item from the total (y/n)? ")
    if xcld == "y":
      frwo = int(input("From which order? "))
      wiwe = int(input("Which item will be excluded? "))
    elif xcld == "n":
      print(f"\nOrder for party of {cust}")
      ordr = 0
      while ordr != cust and cust <= 3:
        ordr = ordr + 1
        print(f"Order {ordr}:")
        if main == 1:
          main = "Chicken"
        elif main == 2:
          main = "Pork"
        elif main == 3:
          main = "Fish"
        elif main == 4:
          main = "Beef"

        if side == 1:
          side = "Steamed Rice"
        elif side == 2:
          side = "Shredded Corn"
        elif side == 3:
          side = "Mashed Potatoes"
        elif side == 4:
          side = "Steamed Vegetables"

        if drnk == 1:
          drnk = "Mineral Water"
        elif drnk == 2:
          drnk = "Iced Tea"
        elif drnk == 3:
          drnk = "Soda"
        elif drnk == 4:
          drnk = "Fruit Juice"
        mtot = mtot + mprc
        stot = stot + sprc
        dtot = dtot + dprc
        print(f"Main: {main}  Price: {mprc}")
        print(f"Side: {side}  Price: {sprc}")
        print(f"Drink: {drnk}  Price: {dprc}\n")

      print(f"Main: {mtot}\nSide: {stot}\nDrinks: {dtot}")
elif strt == 2:
  print("Thank you for using the program.")

如果我为两个客户订购,为订单 1 输入 1、2 和 3,为订单 2 输入 3、2 和 1,则总计只显示两个订单的食品项目 1、2 和 3,而不是所需的输出订单 1 的项目 1、2、3 和订单 2 的项目 3、2 和 1.

python python-3.x if-statement while-loop nested-loops
1个回答
0
投票

实际上您需要某种内存(元组、数组等)来跟踪订单详细信息,您打印相同订单的原因是您每次捕获时都会覆盖变量

main
side
drnk
用户输入并丢弃之前的输入

© www.soinside.com 2019 - 2024. All rights reserved.