Skip to content

Instantly share code, notes, and snippets.

@BlairCurrey
Created March 9, 2021 15:54
Show Gist options
  • Save BlairCurrey/d7b2e3f87610703b2f34af36579e2c3a to your computer and use it in GitHub Desktop.
Save BlairCurrey/d7b2e3f87610703b2f34af36579e2c3a to your computer and use it in GitHub Desktop.
# This command-line test gets an input from the user and
# attempts to square it. If there are no issues it
# prints the result of squaring the input. If the input
# is not an integer a ValueError is raised which triggers
# the 'except' which prints the issue to the user. If another
# exception occurs it will not be caught and will terminate the
# program and print the error message to the console.
while True:
val = input("Enter a number to square: ")
try:
squared = int(val) ** 2
print("You number squared: ", squared)
except ValueError:
print("Number must be an integer.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment