0

I am trying to understand why the output print(idx,item) is not given back on the shell, when I run this program in a python module.

def price_of_items():
       items=["pear","apple","grape"]
       prices_items=[]
       for i in items:
           price=int(input("Please enter the price of",+item))
           price_items.append(price)
       for idx, item in enumerate(price_items):
           print(idx,item)
       return

def main():
       option=""
       while option not in "X": #Loop control for when user doesnt exit program
           print("Main Menu")
           print()
           print("A/ItemPricing")
           print("X. Exit")
           option=input("Please select an option A or X from the Menus above")
           option=option.upper()
           while option not in ("A","X"):
               print("Invalid Input")
               option=input("Select either from A or from X")
               option-option.upper()
           if option=="A":
               price_of_items()
           else:
               exit()
   main()
4
  • use while option != 'X' because if you check if an empty string is in another string, well, then it will be there because it doesn't take up any space Commented Nov 16, 2021 at 8:35
  • is main called recursively from main? Commented Nov 16, 2021 at 8:37
  • 1
    while option not in "X" is wrong, you never go into the loop. Use while option != "X" Commented Nov 16, 2021 at 8:38
  • 2
    The code as currently shown would give an indentation error. Any other expectation of its behaviour is based on guessing how it is actually supposed to be indented. Please post a minimal reproducible example. Commented Nov 16, 2021 at 8:38

1 Answer 1

2

I've edited your code a bit so it will work without errors:

  • You are refrencing item without it being defined.
  • The while loop should be while option != "X"
  • The list you are refrencing in the function should be prices_items

Edited version:

def price_of_items():
    items = ["pear", "apple", "grape"]
    prices_items = []
    for item in items:
        price = int(input(f"Please enter the price of {e} "))
        prices_items.append(price)
    for idx, item in enumerate(prices_items):
        print(idx, item)
    return


def main():
    option = ""
    while option !=  "X":  # Loop control for when user doesnt exit program
        print("Main Menu")
        print()
        print("A/ItemPricing")
        print("X. Exit")
        option = input("Please select an option A or X from the Menus above ")
        option = option.upper()
        while option not in ("A", "X"):
            print("Invalid Input")
            option = input("Select either from A or from X")
            option = option.upper()
        if option == "A":
            price_of_items()
        else:
            exit()


main()

Output flow:

Main Menu

A/ItemPricing
X. Exit
Please select an option A or X from the Menus above a
Please enter the price of pear 12
Please enter the price of apple 13
Please enter the price of grape 14
0 12
1 13
2 14
Main Menu

A/ItemPricing
X. Exit
Sign up to request clarification or add additional context in comments.

1 Comment

you kinda should add a bit of explanation as to what and why you changed exactly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.