I am trying to make a simple calculator, and am trying to return back to the beginning of the loop if they enter an un allowed value--negative numbers-- but I cannot figure out how to return the the beginning of the loop while already midway through it without any errors.
This is what I have so far:
while True:
try:
height = float(input("Enter the height of the cylinder. "))
if height <0:
print("Only enter positive numbers.")
else:
radius = float(input("Enter the radius of the cylinder. "))
if radius <0:
print("Only enter positive numbers.")
finally:
volume = height*3.14159*radius**2
print("The volume of the cylinder is",volume,"units")
I am new to python so I am sorry if this is a really stupid question or if this has already been asked multiple times but I have tried searching but haven't been able to understand any of the other answers. Thank you
CodePudding user response:
Use the continue continue statement.
It will continue with the next iteration of the loop, or as you put it, return you to the begining of the loop.
while True:
try:
height = float(input("Enter the height of the cylinder. "))
if height < 0:
print("Only enter positive numbers.")
continue
else:
radius = float(input("Enter the radius of the cylinder. "))
if radius < 0:
print("Only enter positive numbers.")
continue
finally:
volume = height * 3.14159 * radius**2
print("The volume of the cylinder is", volume, "units")
CodePudding user response:
You cannot simply use continue
like this:
while True:
try:
height = float(input("Enter the height of the cylinder. "))
if height < 0:
print("Only enter positive numbers.")
continue
else:
radius = float(input("Enter the radius of the cylinder. "))
if radius <0:
print("Only enter positive numbers.")
continue
finally:
volume = height*3.14159*radius**2
print("The volume of the cylinder is",volume,"units")
The above solution has a problem. In python, the finally
clause is executed even after the continue
, return
or break
statements. So, if you do this and enter a negative number as the height then it will print Only enter positive numbers
and proceeds to ignore everything and moves to the finally
clause to execute it. The problem with this is that the radius
is defined inside the else
clause. And the program skipped the else
clause due to the continue
statement. So, you cannot calculate the volume in the finally
clause. finally
block is always executed after leaving the try
block. So, finally
block is mostly used for de-allocating system resources before terminating the program if an exception occurs. The major uses of finally
block are:
- Whenever a current method terminates abruptly, there are chances that the method may have been using resources that are allocated to it and will not be freed or closed upon the termination of the current method.
- In such situations finally, a block of code is used to free the resources that were allocated in the try block of code and this finally block of code is always executed regardless of the exception being handled by the except block or not.
- If an exception is not handled by the except the block of code after the control leaves the try block of code, the exception is raised again after the completion of the execution of finally block of code.
- The system resources that needs to be de-allocated is done by the finally block of code.
- Finally block of code can be used right after the try block of code without the except block of code but the exception that is going to be raised will not be handled.
So, I would advise you to not write your output in the finally
block.
You can try the following:
while True:
try:
height = float(input("Enter the height of the cylinder."))
if height < 0:
print("Only enter positive numbers.")
continue
else:
radius = float(input("Enter the radius of the cylinder."))
if radius < 0:
print("Only enter positive numbers.")
continue
volume = height*3.14159*radius**2
print("The volume of the cylinder is",volume,"units")
finally:
print("Press any key to continue and e to exit.")
key = input()
if key == 'e':
break