This input should be a valid integer and must be between 1 and 15 only. If the user enters invalid input, the default level is Level 10. How would I code this?
def check_input():
user_str = input("Enter your value:")
try:
if (str(int(user_str)) == user_str and
1 <= int(user_str) <= 15):
return int(user_str)
else:
return 'Level 10'
except ValueError:
return 'Level 10'
"""
example work
d = check_input()
Enter your value:21
d
'Level 10'
d = check_input()
Enter your value:ggfff
d
'Level 10'
d = check_input()
Enter your value:10
d
10
"""
Comments
Leave a comment