Hi there,
I'm just trying to apply what I've learned so far. I just thinking of a way to add food choice to the current menu. I want my code to allow 4 food choices to be added to the current menu. I'm looking to improve on my basic coding ability, this is what I've come up with so far, its wrong, can someone guide me how do i improve on it?
Thanks
while True:
tries = ""
add_menu = ""
menu = ("salad, pasta, sandwich, pizza, drinks")
add_menu = input("Enter input: ")
add_menu = menu +", " + add_menu
tries += 1
if tries == 4:
print(add_menu)
break
The purpose of having the "+=1" is to tell the code that one choice has been undertaken and when tries == 4, print out the menu with the 4 new choices
A few things:
1. Why do you redeclare menu
each time in the loop? Surely you only need to declare it once outside?
2. You've been told this in another thread, but if you want several items, you don't need to put them in a single string; you can use a list, tuple or set instead. If you are going to use a string, you don't need the parens on line 4.
3. You have several problems with the tries
variable:
- On line 2, you initialise it with an empty string. Why and not a 0? With the code as it is, you should be seeing an exception on line 7, because you can't add an int to a string.
- The variable is redeclared each time in the loop, so even if you had initialised it to 0, it would be reset to 0 each iteration, meaning that on every iteration it would be set to 1 on line 7 and so the expression tries == 4
would never be true. Again, surely you want to declare it before the loop with its initial value.
4. You really don't need line 3, since you're immediately overwriting the value of add_menu
with the value you get from input
on line 5.
5. Think about line 6 and the menu
variable. Do you think that what you've written there is correct?
Thanks for taking the time to explain the concepts. I understand it better now as to why you suggested the things you do. No one explains these concepts when u go through an online lesson. Thanks!
As to point 5, I learn from the previous assignment I did as below.
def add_report(report):
total = 0
items = ""
user = input("Enter an integer to add to toal or Q")
while True:
if report.upper() == "T":
user_1 = input("Enter integer or Q to quit")
if user_1.isdigit():
total = int(user_1) + total
elif user_1.upper() == "Q":
print("\nTotal:\n",total)
break
elif report.upper() == "A":
user_1 = input("Enter integer or Q to quit")
if user_1.isdigit():
total = int(user_1) + total
items = items + "\n" + user_1
elif user_1.upper() == "Q":
print("\nItems\n",items,"\n","Total: ","\n",total)
break
else:
print("Invalid input")
add_report("A")
There's this line, "
items = items + "\n" + user_1 ", my understanding is this will loop the numbers as I input them and it will be called when this " print("\nItems\n",items,"\n","Total: ","\n",total) " is being executed. So I thought in my menu code, as I input the 4 choices, all these choices will be added but seems like it's not working. I'm kinda stuck now
If you do line for line what ndc85430 explained, you're "almost" there.
If the course you are taking has dealt with lists, then redefine the menu = ("....") as a list.(post 2, point 2)
That will be a major step forward.
Paul
If you were to use a list instead of a string as has been suggested:
menu = ['salad', 'pasta', 'sandwich', 'pizza', 'drinks']
you can use
len
to count how many items are in the starting list:
stating_menu_count = len(menu)
to add items to a list, use the list method append:
menu.append('spam')
by getting a new list count you can now calculate how many items have been added to the list:
added_menu_count = # ill leave you to work out this part