Code isn't looping properly in a function

Viewed 29

So I'm currently scripting an ordering system as a Python project. My plan is to allow customers to switch between menus and order items from those menus when they are prompted to. Here's the code I'm working on so far:

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()

When the system asks "Enter an Item Code or [X] to exit.", and after I provide the input, I expect it to print the Item Name as well as the price:

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

But instead, it only asks the question again, nothing else. What am I missing here?

1 Answers

The problem you have is that you are iterating over text lines but do not save them in memory or seeking your cursor There is 2 possible solutions.

1. Add menu.seek(0) at the end of for:

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. Make menu an array containing file lines as follows

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()

P.S. your code looks very bad, sorry)

Related