Skip to content

Instantly share code, notes, and snippets.

@harfordt
Created August 22, 2015 04:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harfordt/e26dce50ffa5d1514916 to your computer and use it in GitHub Desktop.
Save harfordt/e26dce50ffa5d1514916 to your computer and use it in GitHub Desktop.
# do calculations inside he try/catch
while True:
try:
length=int(input("\nPlease enter the vertical length of the square (in centimetres): "))
if length<=0 or length>150:
print("Please enter a number between 1-150.")
continue
p_square=length*4
a_square=length*length
print("\nThe perimeter of your square is {} centimetres.".format(p_square))
print("\nThe area of your square is {}.".format (a_square))
break
except ValueError:
print("Please enter integers only.")
# use the try/catch *only* to take the input, do calculations once you have a valid value
while True:
try:
length=int(input("\nPlease enter the vertical length of the square (in centimetres): "))
if length<=0 or length>150:
print("Please enter a number between 1-150.")
continue
break
except ValueError:
print("Please enter integers only.")
p_square=length*4
a_square=length*length
print("\nThe perimeter of your square is {} centimetres.".format(p_square))
print("\nThe area of your square is {}.".format (a_square))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment