While loop not working


count_two = 0
count_3_12 = 0
count_65 = 0
count_rest = 0
number_of_guests = 1
sum_of = 0
count = 0
price_count_two = 0
price_3_12 = 14
price_65 = 18
price_rest = 23

while number_of_guests != 0:
    number_of_guests = int(input("Enter number of guests: "))
    count += number_of_guests
    print("count",count)
    age = int(input("Enter age of the guest: "))
    if age <= 2:
        count_two = count_two + 1
        print ("count_two",count_two)
    elif age >= 3 and age <= 12:
        count_3_12 = count_3_12 + 1
        print ("count_3_12",count_3_12)
    elif age >=65:
        count_65 = count_65 + 1
        print("count_65",count_65)
    else:
        count_rest = count_rest + 1
        print("count_rest",count_rest)

total_count_two = count_two * price_count_two
total_count_3_12 = count_3_12 * price_3_12
total_count_65 = count_65 * price_65
total_count_rest = count_rest * price_rest

print ("total 2 years and less", total_count_two)
print("total 3 years to 12 years",total_count_3_12)
print("total over 65",total_count_65)
print("total rest",total_count_rest)

This while loop is not performing its function
When l enter zero it still carries on with the rest of the code below it
Please help
Thanks

A while loop would only check the conditional when first met and then on each new iteration. Changing the value within the nested code of the loop would not automatically exit the while loop as the conditional is not immediately checked; it would only be checked again once that suite had finished executing.

So check condition; if true- execute statements, check condition, … and so on.

This is what it is showing, so you suggest l put

this outside the loop to check the condition, but it still doesn’t work, it shows this

Redeclared ‘number_of_guests’ defined above without usage

If you move the number_of_guests input up 3 lines, it works for 0. Any non 0 number results in an infinite loop though.
Your code will loop so long as number_of_guests != 0 so as you define your guests with each loop, that variable would need to eventually zero out to end your while loop.


# keeps count age wise
count_two = 0
count_3_12 = 0
count_65 = 0
count_rest = 0

#keeping track of total guests
number_of_guests = 1




# keeps track of number of guests processed in each iteration
count = 0

# prices age wise
price_count_two = 0
price_3_12 = 14
price_65 = 18
price_rest = 23

number_of_guests = int(input("Enter number of guests: "))


while number_of_guests != 0:
    for guests in number_of_guests:

        count += guests
        print("count",count)
        age = int(input("Enter age of the guest: "))
        if age <= 2:
            count_two = count_two + 1
            print ("count_two",count_two)
        elif age >= 3 and age <= 12:
            count_3_12 = count_3_12 + 1
            print ("count_3_12",count_3_12)
        elif age >=65:
            count_65 = count_65 + 1
            print("count_65",count_65)
        else:
            count_rest = count_rest + 1
            print("count_rest",count_rest)

total_count_two = count_two * price_count_two
total_count_3_12 = count_3_12 * price_3_12
total_count_65 = count_65 * price_65
total_count_rest = count_rest * price_rest

print ("total 2 years and less", total_count_two)
print("total 3 years to 12 years",total_count_3_12)
print("total over 65",total_count_65)
print("total rest",total_count_rest)


am getting this error please explain

You can’t iterate through a number. Think about it. Take the number 5. Think about for i in 5. What would that mean?

1 Like

I thought it would go like 1, 2, 3, 4 and 5

That’s a range (hint) of numbers. Objects like strings are iterable, which means you can go from one element to the next. You can’t do that with a number.

1 Like

So l have included range in my code but it is not working the way l want, if I write there are 2, in number of guests, then l just want for each guest, it records the age and prints the total admission cost, in my program, it keeps asking age infinitely


# keeps count age wise
count_two = 0
count_3_12 = 0
count_65 = 0
count_rest = 0

#keeping track of total guests
number_of_guests = 1




# keeps track of number of guests processed in each iteration
count = 0

# prices age wise
price_count_two = 0
price_3_12 = 14
price_65 = 18
price_rest = 23

number_of_guests = int(input("Enter number of guests: "))


while number_of_guests != 0:
    for number_of_guests in range (0, number_of_guests + 1):

        count += 1
        print("count",count)
        age = int(input("Enter age of the guest: "))
        if age <= 2:
            count_two = count_two + 1
            print ("count_two",count_two)
        elif age >= 3 and age <= 12:
            count_3_12 = count_3_12 + 1
            print ("count_3_12",count_3_12)
        elif age >=65:
            count_65 = count_65 + 1
            print("count_65",count_65)
        else:
            count_rest = count_rest + 1
            print("count_rest",count_rest)

total_count_two = count_two * price_count_two
total_count_3_12 = count_3_12 * price_3_12
total_count_65 = count_65 * price_65
total_count_rest = count_rest * price_rest

print ("total 2 years and less", total_count_two)
print("total 3 years to 12 years",total_count_3_12)
print("total over 65",total_count_65)
print("total rest",total_count_rest)

while number_of_guests != 0. When will number_of_guests equal zero?

when we don’t want anything to be done

Your while loop doesn’t modify number_of_guests so once you enter the loop there’s no getting out.

You could think of a while loop in this application like a line at a store. When you finish checking someone out, they leave the line. When the line is empty, the cashier stops working. It’s similar to a for loop but allows more mutability. If your hotel didn’t accept minors your program could refuse to accept ages under 18 by not modifying your while loop variable for that input range. It also allows you to exit the loop more rapidly in defined circumstances: “is anyone under 13 or over 64?”

Another note: range creates a list that starts with the first number and ends before the second number, aka range(inclusive, exclusive). range(0, 3) would be the list [0, 1, 2]

1 Like

Note, in Python 3 range() creates a range object, which is an iterable but not a list:

print(range(0,3))
#output: range(0,3)

In Python 2; however, you are correct. range() creates a list object.

1 Like

oops, forgot about that, thanks!

1 Like

It is not clear to me where l am wrong in the code above,

I don’t get what you are saying here

Thanks

number_of_guests = 1
while number_of_guests != 0:
  variable = input("Press enter to see this loop again")

I suspect you’re thinking of a while loop like a for loop. In an effort to not just give you the answer, I suggest you run this code and think about how to make it stop. You have to satisfy while number_of_guests != 0:

1 Like

if l could think a solution for it I would have not come to forums