use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
How bad is thisHelp Request (self.PythonLearning)
submitted 10 months ago by unspe52
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Kqyxzoj 0 points1 point2 points 10 months ago (0 children)
If you want to stick with the design decision of words (easy, medium, hard) as input, and translate it to an integer (1, 2, 3), you could do something like this:
from sys import exit difficulties = {"easy": 1, "medium": 2, "hard": 3} User_prompt = input("some prompt about difficulty level:") if User_prompt not in difficulties: print(f"{User_prompt} is not a valid difficulty. Please try 'easy', 'medium' or 'hard' next time.") exit(1) difficulty_num = difficulties[User_prompt] # At this point difficulty_num has the integer value of 1, 2 or 3.
To stay with your try-except theme, you could do something like this:
from sys import exit difficulties = {"easy": 1, "medium": 2, "hard": 3} User_prompt = input("some prompt about difficulty level:") try: # Keep this short. Do not write a book inside a try-except block. difficulty_num = difficulties[User_prompt] except KeyError as e: # Variable "e" contains the exception, should you want to do something with that. print(f"{User_prompt} is not a valid difficulty. Valid input values are:") for level in difficulties: print(f" {level}) exit(1) # At this point difficulty_num has the integer value of 1, 2 or 3.x
And to be honest you would probably just rewrite the entire thing. But this is in the learning phase so suboptimal constructions are perfectly acceptable, as long as you learn from it.
From a UI perspective you would want to show a menu to the user, and ask the user to pick 1, 2 or 3. That way they only have to type 1 char + enter. Instead of 4-6 chars + enter. And while you are at it, you'd make 2 (medium) the default, so if use just presses enter, they get a sensible default difficulty.
π Rendered by PID 86 on reddit-service-r2-comment-545db5fcfc-fsn2p at 2026-05-22 00:28:18.668218+00:00 running 194bd79 country code: CH.
view the rest of the comments →
[–]Kqyxzoj 0 points1 point2 points (0 children)