[Python] using while loop to check errors with inputs in a list

Discussão em 'Python' iniciado por Stack, Fevereiro 1, 2025.

  1. Stack

    Stack Membro Participativo

    I've been making a program on Python...

    selected_pizzas=[] #creates an empty list
    for n in range(pizza_number):
    selected_pizzas = selected_pizzas + [int(input("Choose a pizza: "))]


    that will let the user input up to 5 numbers and store them in an empty list (selected_pizzas) the the user is only allowed to enter numbers from 0-11, I had a method of using while loops to check errors with the users inputs but I tried and it didn't seem to work with the numbers being inputted in the list:

    pizza_number=0
    goodpizza_number=False
    while not goodpizza_number:
    try:
    pizza_number= int(input("How many Pizzas do you want? (MAX 5): "))
    if pizza_number ==0 or pizza_number > 5: #if the number of pizzas asked for is 0 or over 5 it will give an error message and repeat question
    print("Not a correct choice, Try again")
    else:
    goodpizza_number=True
    except ValueError:
    print("Not a number, Try again")


    How would I use this method (above) to make sure the user is inputting the expected inputs(numbers from 0-11) in the first piece of code shown in the question? Thanks

    Continue reading...

Compartilhe esta Página