hey guys can some one help with this loop it is getting to the first if and get stuck appreciating your help
Options = int(input('Enter an Options :'))
while Options != 0:
if Options == 1:
item = input('enter the item : ')
qnty = int(input('Enter the Quantitiy for the item : '))
Shoping_list[item] = qnty
elif Options == 2:
for item in Shoping_list:
print(item, ':', Shoping_list[item])
item = input('Enter the item you want to Remove : ')
del(Shoping_list[item])
elif Options == 3:
for item in Shoping_list:
print(item, ':', Shoping_list[item])
elif Options != 0:
print('you didnt enter a valid number ')
else:
print('shopping list is close')
In each of your if statements, insert Options = 0 at the end. Since your while loop depends on options not being 0. Resetting it to 0 allows the user to pick another option.
while Options != 0:
if Options == 1:
item = input('enter the item : ')
qnty = int(input('Enter the Quantitiy for the item : '))
Shoping_list[item] = qnty
Options = 0
Also, as a tip, make sure your spelling and grammar are accurate, and that you are consistent with spacing. It makes it a lot easier for others to read your code.
Here's a working example of the correct if loops. The user can modify the dictionary with the if loops, and can run each of them one after another.
Shoping_list = {}
while True:
Options = int(input('Enter an Options :'))
while Options != 0:
if Options == 1:
item = input('enter the item : ')
qnty = int(input('Enter the Quantitiy for the item : '))
Shoping_list[item] = qnty
Options = 0
elif Options == 2:
for item in Shoping_list:
print(item, ':', Shoping_list[item])
item = input('Enter the item you want to Remove : ')
del(Shoping_list[item])
Options = 0
elif Options == 3:
for item in Shoping_list:
print(item, ':', Shoping_list[item])
Options = 0
elif Options != 0:
print('you didnt enter a valid number ')
else:
print('shopping list is close')
insert the first statement Options = int(input('Enter an option'))
inside the while loop.
while options!=0:
Options = int(input('Enter an option'))
.
.
.
.
Your code is a bit hard to read and hard to maintenance I suggest, change the 'while loop' to loop infinitely when you want to exit, simply break the loop and I prefer to show a menu before asking an option.
You can change your code like this:
def display_menu():
print("1. Add a new item to shopping list")
print("2. Remove an item")
print("3. Print Shopping List Items")
print("0. Exit")
return int(input('Enter an Options (0~3):'))
while True:
option = display_menu()
if option == 1:
item = input('enter the item : ')
qnty = int(input('Enter the Quantitiy for the item : '))
Shoping_list[item] = qnty
elif option == 2:
for item in Shoping_list:
print(item, ':', Shoping_list[item])
item = input('Enter the item you want to Remove : ')
del(Shoping_list[item])
elif option == 3:
for item in Shoping_list:
print(item, ':', Shoping_list[item])
elif option == 0:
print('shopping list is close')
break # Exit menu
else:
print('you didnt enter a valid number ')
Awesome thanks a lot guys ! Im new to python this info very helpful
def display_menu():
print("1. Add a new item to shopping list")
print("2. Remove an item")
print("3. Print Shopping List Items")
print("0. Exit")
return int(input('Enter an Options (0~3):'))
while True:
option = display_menu()
if option == 1:
item = input('enter the item : ')
qnty = int(input('Enter the Quantitiy for the item : '))
Shoping_list[item] = qnty
elif option == 2:
for item in Shoping_list:
print(item, ':', Shoping_list[item])
item = input('Enter the item you want to Remove : ')
del(Shoping_list[item])
elif option == 3:
for item in Shoping_list:
print(item, ':', Shoping_list[item])
elif option == 0:
print('shopping list is close')
break # Exit menu
else:
print('you didnt enter a valid number ')
Ps like the option with the function you can call
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.