-
Notifications
You must be signed in to change notification settings - Fork 0
/
age-quiz.py
70 lines (61 loc) · 2.56 KB
/
age-quiz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
""" This is an if-elif-else statement that prints out different
messages according to the user's age. """
# Arrange if-elif statements in descending order regarding age.
# If the condition, 'age >= 40', was put as the starting if statement,
# the flow of logic would overide the next elif statements
# >= 65 and > 100 and their respective messages would not be output.
# This is because the conditional, 'age >=40', covers all numbers over 40,
# so the flow of logic would output the first conditional
# and not need to output the next two.
# Open the file in read mode
# 'drivers.txt' filename was changed to 'drivers-info.txt' manually,
# pathname had been altered to show the filename change
with open('/Users/charmainefernandes/Documents/Documents/Cogrammar/\
CF23100010231/Data Science (Fundamentals)/T23 - Version Control and \
Git/Projects/Easiest Age calculator/age-quiz/driver-info.txt', 'r') as file:
# Read the drivers from the file
drivers = file.readlines()
# Process each line (assuming each line contains information about a driver)
for driver_info in drivers:
# Process driver_info as needed
print(driver_info.strip()) # For example, printing each line
# int() function casts users age input, from string to an integer.
age = int(input("Please enter your age: "))
if age > 100:
print("Sorry, you're dead.")
elif age >= 65:
print("Enjoy your retirement!")
elif age >= 40:
print("You're over the hill.")
elif age < 13:
print("You qualify for the kiddie discount.")
elif age == 21:
print("Congrats on your 21st!")
else:
print("Age is but a number.")
"""This is an alternative code
with an if statement nested within another
if statement and while loop allowing user to keep
entering age until '-1' is entered to exit."""
while True:
# Exception handling added if user doesn't enter a valid number.
try:
age = int(input("Please enter your age or '-1' to exit: "))
if age >= 40:
if age >= 65 and age <= 100 :
print("Enjoy your retirement!")
elif age > 100:
print("Sorry, you're dead.")
else:
print("You're over the hill.")
elif age < 13 and age != -1:
print("You qualify for the kiddie discount.")
elif age == 21:
print("Congrats on your 21st!")
elif age == -1:
print("Thank you for using the age quiz.")
break
else:
print("Age is but a number.")
except ValueError:
print("Error: Please enter a valid digit. Non-digit characters are not allowed.")