-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode2.py
More file actions
22 lines (19 loc) · 856 Bytes
/
Code2.py
File metadata and controls
22 lines (19 loc) · 856 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
'''Objective: To identify and fix errors in a Python program that validates user input.'''
'''
'age' is initially assigned as a string. When you use input(), the user's input is always a string. Therefore, we need to convert age to an integer before comparing it with 18.
The comparison age >= 18 would cause a TypeError because age is a string, not an integer. To fix this, we should convert age to an integer before performing the comparison.
'''
def get_age():
age = input("Please enter your age: ")
if age.isnumeric() and int(age) >= 18: #Fixed comparison and conversion
return int(age)
else:
return None
def main():
age = get_age()
if age:
print(f"You are {age} years old and eligible.")
else:
print("Invalid input. You must be at least 18 years old.")
if __name__ == "__main__":
main()