Your markup is broken: you used two backticks to start the code block,
but you need to use three, and they should be on their own line.
Did you make this post on the website? I’m surprised that you didn’t
notice that the formatting was lost.
You suggested that the line
age = 1 was an unnecessary line of code.
It is not unnecessary. If you take it out, the while loop cannot test
whether or not age equals zero because age doesn’t exist. So the
simplest solution that works is to do what you already did: initialise
the age variable before the loop.
This works, is efficient, simple and understandable. Any other solution
will just be harder to read and write.
One alternative would be to change the loop to an infinite loop and
forget about testing the age in the while condition. You already have an
exit in the middle of your loop, so that’s not too bad*.
age = float(input("Enter your age"))
if age == 0.0:
sys.exit("Process finished with exit code 0")
although I’ll be honest, looking at that sys.exit in the middle of your
while loop makes me come out in hives wink but since I can’t see the
rest of your code I don’t know if that’s justified or not.
I can tell you though that your exit command is severely broken. Your
- prints the message “Process finished with exit code 0”
- and then exits with exit code 1 (failure)
which makes the message a lie. You can check this for yourself by
running your script and then checking the exit code.
In bash (under Linux), you can check the exit code with:
In Windows cmd.exe, I think you can use:
and in Powershell I think that you can use
If you have no code after your while loop, then I recommend that you
sys.exit(...) line with a simple