Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
indefinite loop
#1
Hello everyone
I am new to Python and I am learning with a book I bought. In this book there are some do it yourself questions which are much more complicated than the examples. Now here is the code I wrote:

age = input('Please enter your age. ')
age = int(age)
 
while age != 'quit'
    if age < 3:
        print('your ticket is free')
    elif 3<= age <= 12:
        print('you ticket is 10$')
    elif age > 12:
        print('your ticket is 15$')
This code is supposed to ask the age and based on the age tell the price and there must be a loop ( which is why I used while ) but in the question it didn't ask me to specify how/when to stop the loop so the "while" statement doesn't look OK to me. question 1: How to write a loop for age and not to specify when to stop ( which means I don't want the user to be able to stop the loop. They just need to enter their age ) 2 question: when I run this code it asks for the age (prompt) and then it gives the ticket price statement but it doesn't stop for the second person to enter the age. It shows the :"your ticket is..." indefinitely. Sorry for long message.
Reply
#2
1) You can use a while True: loop and if your exit condition is met use "break"
2) put all your code into an "outer" loop
3) you need to check what the input function returns
age < 3 only works if you entered numbers but what happens if you entered 'quit'?
Reply
#3
There is fundamental flaw in code.

You enter while loop with condition age != 'quit' (you missed : in your code). In the body of while loop you don't change age nor have break statement. This means that while loop will never stop as condition will not become False and loop is not exited in some other way.

I think that if you follow ThomasL advice and re-write you code you can reach working solution.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
Thank you Thomas and perfringo. Thomas I will try your solution to fix the problem. Thank you
perfringo: you are right this code is missing a colon which I dropped while copy pasting but as you mentioned even with that it doesn't work because it is missing other important parts. I will try to say a Thomas said.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Which GUI can have indefinite loop ? jst 20 1,582 Nov-16-2023, 10:23 PM
Last Post: jst
  Init an indefinite number of class MathisDELAGE 9 2,228 Feb-18-2022, 07:49 PM
Last Post: deanhystad
  Indefinite loop ( I think ) marsh20 2 1,884 Aug-20-2020, 12:33 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020