3

I'm relatively new to coding and im super confused at why my code is not working it looks like it should but it doesn't. it first asks for the amount of seats required and then asks for there name then there food choice. it appends the names fine but it only appends the first food choice and nothing after that. I'm not getting any errors anything.

Here's my code:

people = []
working = False
seatNum = input("How many seats do you need: ")
while (not seatNum.isnumeric()):
  print("Invalid")
  seatNum = input("How many seats do you need: ")
print("Mince pie = 1, Chocolate coins = 2, Apple pie = 3")
seatNum = int(seatNum)
for i in range(seatNum):
  Nam = input("Enter name: ")
  people.append(Nam)
  choice = input("Enter your choice: ")
  while (working == False):
    while (not choice.isnumeric()):
      print("Invalid")
      choice = input("Enter your choice: ")
    choice = int(choice)

    if choice == 1 or choice == 2 or choice == 3:
      working = True
    else:
      print("Enter number between 1-3")
      choice = input("Enter your choice: ")

  if choice == 1:
    people.append("Mince Pie")
  elif choice == 2:
    people.append("Chocolate coins")
  elif choice == 3:
    people.append("Apple pie")
print(people)

That's my code is super messy but its not done. here's the output:

How many seats do you need: 3
Mince pie = 1, Chocolate coins = 2, Apple pie = 3
Enter name: a
Enter your choice: 1
Enter name: b
Enter your choice: 2
Enter name: c
Enter your choice: 3
['a', 'Mince Pie', 'b', 'c']

and here is the desired output:

How many seats do you need: 3
Mince pie = 1, Chocolate coins = 2, Apple pie = 3
Enter name: a
Enter your choice: 1
Enter name: b
Enter your choice: 2
Enter name: c
Enter your choice: 3
['a', 'Mince Pie', 'b',  'Chocolate coins', 'c', 'Apple pie']

Any and all help is appreciated. Thanks in Advance

Talia.

4
  • Do you know what a function is? See if you can make one that presents a menu in general, and then use it for each menu choice you want the user to make. Commented Dec 12, 2020 at 2:06
  • 1
    Instead of using the working variable, use while True: and then use break when you want to get out of the loop. Commented Dec 12, 2020 at 2:08
  • 1
    Also, I think it's likely you want to have a list of tuples, or maybe a dictionary as your data stricture. As you seem to want to record the mapping from person to product. A list containing both lacks semantics. Commented Dec 12, 2020 at 2:10
  • i know what functions are but i'm not confident in implementing them as of this moment. It's what i'm planning on learning next. The ` while True: ` works great thanks for suggestion. Commented Dec 12, 2020 at 2:21

3 Answers 3

2
while (working == False):

This while loop will not run for guests after the first, because working was set to True that time around and does not get reset.

choice = int(choice)

This part only happens inside the while loop, so it never happens for guests after the first.

if choice == 1:

Since choice is still a string, these comparisons always fail.

Sign up to request clarification or add additional context in comments.

Comments

2

After you run the '''While(working == False)''' loop. You set working = True inside of the loop. The next time the loop is ran for the next person, working is still equal to True. So the loop will not run. Try this partial code:

#Add your original code
for i in range(seatNum):
Nam = input("Enter name: ")
people.append(Nam)
choice = input("Enter your choice: ")
working = False #This statement is what I added
while (working == False):
  while (not choice.isnumeric()):
    print("Invalid")
    choice = input("Enter your choice: ")
  choice = int(choice)
  #Continue your code normally

This way every time the loop is run the working variable will be False and the loop will be able to run.

Comments

0
if choice == 1 or choice == 2 or choice == 3:
    working = True

This is what was causing the issue. Once the program saw choice was equal to one of those numbers it changed working to True and stopped appending the choice.

people = []
seatNum = input("How many seats do you need: ")


while seatNum.isnumeric() == False:
  print("Invalid")
  seatNum = input("How many seats do you need: ")
seatNum = int(seatNum)

print("Mince pie = 1, Chocolate coins = 2, Apple pie = 3")

while seatNum*2 != len(people):
    Nam = input('Enter Name: ')
    people.append(Nam)
    
    choice = input('What is your choice: ')
    while choice.isnumeric() == False:
        print('Invalid')
        choice = input('What is your choice: ')
    
    choice = int(choice)
    
    if choice == 1:
        people.append("Mince Pie")
    elif choice == 2:
        people.append("Chocolate coins")
    elif choice == 3:
        people.append("Apple pie")
        
print(people)

I took out the working variable to make the logic of your code run better. The logic of the while loop runs until the len(people) is double seatNum since everyone gets one choice of desert.

Comments

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.