I have been working on an assignment which I have almost finished. However, the while loop which is the main body of code doesn't repeat after one of the if statements is completed. I am really struggling to work out why this is...
The if statement in question is as follows:
#Adding new items to the list, viewing list, marking items as bought or viewing total cost
print ("\nIf you would like to add a new item to your list '"+Shopping_List_Name+"', enter 'N'.\nIf you would like to view your list '"+Shopping_List_Name+"', enter 'L'.\nIf you would like to mark an item on your list '"+Shopping_List_Name+"' as 'bought', enter 'B'.\nIf you would like to view the approximate total cost of the items on your list '"+Shopping_List_Name+"' which you have not bought yet.")
flag3 = 0
while flag3 == 0: #Main while loop
flag4 = 0
command = input("\nPlease enter 'N' (new item), 'L' (view list), 'B' (mark item as 'bought') or 'T' (view total cost): ").upper() #upper() function ensures that following IF statements work
if command == "N":
New_Item = New_Item(Shopping_List_Name) #Calls function New_Item
Max_Price = Max_Price(New_Item) #Calls function Max_Price
Quantity = Quantity(New_Item) #Calls function Quantity
Shopping_List = Add_Item(Shopping_List, New_Item, Max_Price, Quantity) #'Shopping_List' is updated by the value returned by the function 'Add_Item'
Total = Total_Cost(Max_Price, Quantity, Total) #'Total' variable is updated by the value returned by the Total_Cost function
This is the rest of the while loop:
elif command == "L":
counter = 0 #Establishes counter value as 0
print("\nList title:",(Shopping_List_Name.capitalize()),"\n")
for i in Shopping_List: #Iterates through the list and prints the position number of each element followed by the respective element
counter = counter+1 #Add 1 to previous value of counter each time function iterates through the list 'Shopping_List'
print(str(counter)+". "+i) #Prints the position number and the element
elif command == "B":
counter = 0 #Establishes counter value as 0
print("\nList title:",Shopping_List_Name,"\n")
for i in Shopping_List: #Iterates through the list and prints the position number of each element followed by the respective element
counter = counter+1 #Add 1 to previous value of counter each time function iterates through the list 'Shopping_List'
print(str(counter)+". "+i) #Prints the position number and the element
while flag4 == 0: #Sets up a loop
Item_Bought = input("\nPlease enter the position number of the item which you would like to mark as 'bought': ")
try: #Tries the following code
Item_Bought = int(Item_Bought) #Allows user to select item they want to mark as bought from the list of items
Item_Bought = Item_Bought-1
Shopping_List[Item_Bought] = Shopping_List[Item_Bought]+" - BOUGHT!" #Adds 'BOUGHT!' to the item in the list chosen by the user (need -1 as list begins at 0)
Item_Moving = Shopping_List[Item_Bought] #Creates variables 'Item_Moving' which contains the item the user has marked as bought
Shopping_List.remove(Item_Moving) #Deletes the item the user has marked as bought from its original position
Shopping_List.append(Item_Moving) #Adds the item the user has marked as bought to the end of the list
flag4 = 1 #Ends loop
except: #if the code fails (user input is not in accepted range), while loop restarts
print("You have entered an invalid input.\n")
Item_Bought_Remove = Shopping_List[Item_Bought].split("-")
Price_Remove = (Item_Bought_Remove[2].split("£"))[1]
Quantity_Remove = (Item_Bought_Remove[3].split(": "))[1]
Total = Total-(Cost_Remove(Price_Remove,Quantity_Remove))
elif command == "T":
print("\nThe approximate total cost of the items you have not yet bought on your list '"+Shopping_List_Name+"' is: £"+str(Total))
else:
print("Oops! '"+command+"' is not an option.") #If user input is not valid
Aucun commentaire:
Enregistrer un commentaire