簡體   English   中英

代碼未在 function 中正確循環

[英]Code isn't looping properly in a function

所以我目前正在編寫一個訂購系統作為 Python 項目。 我的計划是允許客戶在菜單之間切換並在提示時從這些菜單中訂購商品。 這是我目前正在處理的代碼:

def order():
  global input1
  global itemCode
  menu = open("FoodMenu.txt","r")
  while(input1 != "X"):
    for line in menu:
      data = line.split(";")
      itemCode = data[0]
      itemName = data[1]
      itemPrice = float(data[2])
      if itemCode == input1:
       print(itemCode +  " - " + itemName + " - $" + str(itemPrice))
    input1 = input("Enter an Item Code or [X] to exit.")
  
#Start

print("******************* TAKEAWAY ORDERING SYSTEM ******************")
print("                                                               ")
print("         [O] Order                                             ")
print("         [X] Exit                                              ")
print("                                                               ")
print("***************************************************************")

input1 = input("Select an option to begin: ")
while input1 != "O" or input1 != "X":
    if input1 == "O" or input1 == "X":
        break
    input1 = input("Please select from one of the options above: ")

if input1 == "O":
  print("A;Crispy Chicken;5.25")
  print("B;Boneless Box;5.49")
  print("C;Hot Wings;2.19")
  print("D;Chicken Nuggets;3.59")
  print("E;Smoky-Grilled Chicken;6.30")
  order()

當系統詢問“Enter an Item Code or [X] to exit.”時,在我提供輸入后,我希望它會打印項目名稱和價格:

print(itemCode +  " - " + itemName + " - $" + str(itemPrice))

但相反,它只是再次提出問題,沒有別的。 我在這里想念什么?

您遇到的問題是您正在迭代文本行但不將它們保存在 memory 或尋找您的 cursor 有兩種可能的解決方案。

1. 在 for 末尾添加menu.seek(0)

def order():
    global input1
    global itemCode
    menu = open("FoodMenu.txt", "r")
    while (input1 != "X"):
        for line in menu:
            data = line.split(";")
            itemCode = data[0]
            itemName = data[1]
            itemPrice = float(data[2])
            if itemCode == input1:
                print(itemCode + " - " + itemName + " - $" + str(itemPrice))
        input1 = input("Enter an Item Code or [X] to exit.")
        menu.seek(0)


# Start

print("******************* TAKEAWAY ORDERING SYSTEM ******************")
print("                                                               ")
print("         [O] Order                                             ")
print("         [X] Exit                                              ")
print("                                                               ")
print("***************************************************************")

input1 = input("Select an option to begin: ")
while input1 != "O" or input1 != "X":
    if input1 == "O" or input1 == "X":
        break
    input1 = input("Please select from one of the options above: ")

if input1 == "O":
    print("A;Crispy Chicken;5.25")
    print("B;Boneless Box;5.49")
    print("C;Hot Wings;2.19")
    print("D;Chicken Nuggets;3.59")
    print("E;Smoky-Grilled Chicken;6.30")
    order()

2. 將menu設為包含文件行的數組,如下所示

def order():
    global input1
    global itemCode
    menu = open("FoodMenu.txt", "r").readlines()
    while (input1 != "X"):
        for line in menu:
            data = line.split(";")
            itemCode = data[0]
            itemName = data[1]
            itemPrice = float(data[2])
            if itemCode == input1:
                print(itemCode + " - " + itemName + " - $" + str(itemPrice))
        input1 = input("Enter an Item Code or [X] to exit.")



# Start

print("******************* TAKEAWAY ORDERING SYSTEM ******************")
print("                                                               ")
print("         [O] Order                                             ")
print("         [X] Exit                                              ")
print("                                                               ")
print("***************************************************************")

input1 = input("Select an option to begin: ")
while input1 != "O" or input1 != "X":
    if input1 == "O" or input1 == "X":
        break
    input1 = input("Please select from one of the options above: ")

if input1 == "O":
    print("A;Crispy Chicken;5.25")
    print("B;Boneless Box;5.49")
    print("C;Hot Wings;2.19")
    print("D;Chicken Nuggets;3.59")
    print("E;Smoky-Grilled Chicken;6.30")
    order()

PS你的代碼看起來很糟糕,對不起)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM