Answer to Question #177774 in Python for knotaima

Question #177774

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?


1
Expert's answer
2021-04-02T09:34:51-0400
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
"""

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS