Skip to content

Commit 1aa79d1

Browse files
committed
added num_guess_try_catch.py
1 parent 6d79fd8 commit 1aa79d1

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

num_guess_try_catch.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# ...existing code...
12
#!/usr/bin/env python3
23

34
# Created by: Angelo Garcia
@@ -6,19 +7,22 @@
67

78

89
def main():
9-
# This function uses a try statement to catch input errors
10+
# This function demonstrates validating user input with try/except/finally
1011

11-
# input
12+
# input - prompt the user for the number of students (input returns a string)
1213
integer_as_string = input("Enter the number of students: ")
1314
print("")
1415

1516
# Process & output
1617
try:
18+
# Try to convert the input string to an integer
1719
integer_as_number = int(integer_as_string)
18-
print("you entered an integer correctly!")
20+
print("You entered an integer correctly!")
1921
except Exception:
22+
# If conversion fails, inform the user the input was not an integer
2023
print("That was not an integer!")
2124
finally:
25+
# This always runs whether conversion succeeded or failed
2226
print("Thank you for playing.")
2327

2428

0 commit comments

Comments
 (0)