Reputation: 3
I am trying to make a simple calculator and want to return to the beginning of the loop if an unallowed value, like negative numbers, is input. However, I cannot figure out how to return to the beginning of the loop while already midway through 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 have tried searching and haven't been able to understand the other answers.
Upvotes: 0
Views: 134
Reputation: 1017
You need except
statement to handle ValueError
exception when input is not float/int
after try
block. The finally
clause runs whether or not the try
statement produces an exception, so you might not need that.
To handle non positif number, you need continue
statement on each if
to go back or restart to while
loop statement. But if don't want to use continue
, you can change if
statement to while
statement combine with walrus operator (:=
) and or
statement to evaluate input
. This will reduce the line and repeated code.
When the execution reach final calculation and print
statement, you need to add break
to stop looping back to the beginning. And lastly, I would change < 0 to <= 0 to avoid 0 or negative value in input.
while True:
try:
while (height := float(input("Enter the height of the cylinder. "))) <= 0 \
or (radius := float(input("Enter the radius of the cylinder. "))) <= 0:
print("Only enter positive numbers.")
print(f'The volume of the cylinder is {(volume := height*3.14159*radius** 2)} units')
break
except ValueError:
print("Invalid numbers")
while
statement will evaluate try
except
block to handle
ValueError
when input
is not numbers (float/int)while
will evaluate and handle non positif numbers in input
:=
) creates an assignment expression. The operator
allows to assign a value to a variable inside an expression.Break
will stop the loop when the condition satisfiedUpvotes: 0
Reputation: 27008
You should also consider other types of invalid input - i.e., anything that cannot be converted to float
import math
while (hos := input('Enter height of cylinder or <return> to end: ')):
try:
if (hos := float(hos)) < 0:
raise ValueError('Must be positive')
if (ros := float(input('Enter radius of the cylinder: '))) < 0:
raise ValueError('Must be positive')
volume = hos * math.pi * ros ** 2
print(f'Volume of cylinder is {volume:.2f} units')
except ValueError as e:
print(e)
Upvotes: 1
Reputation: 72
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:
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
Upvotes: 2
Reputation: 6637
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")
Upvotes: 0