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()
kiner_shah
4,8337 gold badges30 silver badges47 bronze badges
1 Answer 1
I've edited your code a bit so it will work without errors:
- You are refrencing
itemwithout it being defined. - The
whileloop should bewhile 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
answered Nov 16, 2021 at 8:38
David Meu
1,5309 silver badges19 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Matiiss
you kinda should add a bit of explanation as to what and why you changed exactly
lang-py
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 spacewhile option not in "X"is wrong, you never go into the loop. Usewhile option != "X"