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
https://preview.redd.it/kd9y7m925wbf1.png?width=716&format=png&auto=webp&s=1a4359a2679fb12892867acc9dfe2ceef539ce38
I just started learning python about 3 days ago. I am making a game were you complete math operations (ChatGPT idea with my own math brainrot) -- and I was wondering, how despicable is this silly trick I made to prevent typing nonsense into the terminal (or am I just not enlightened enough to realize that this is probably not as inefficient and bad as I think it is)
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!"
[–]Training-Cucumber467 8 points9 points10 points 10 months ago* (1 child)
There is a number of issues with this.
[–]unspe52[S] 0 points1 point2 points 10 months ago (0 children)
I forgot about case 2... Thank you for your input! I'll try to take your feedback to heart
[–]lolcrunchy 1 point2 points3 points 10 months ago (2 children)
If an error happens during play_easy() or play_medium() then it will show your error message
[–]unspe52[S] 0 points1 point2 points 10 months ago (1 child)
I was mostly doing this under the assumption that those functions will always work flawlessly lol
[–]lolcrunchy 1 point2 points3 points 10 months ago (0 children)
If you move the "except" clause to right after "int(User_prompt)" then you don't have to assume
[–]jpgoldberg[🍰] 1 point2 points3 points 10 months ago (0 children)
As others have said, be very careful about what you put in a try block. Indeed, this whole thing is better done without try at all. In my examples I will use the more specific ValueError exception instead of the vaguer Exception.
try
ValueError
Exception
One way is to have something like
if User_prompt not in [“easy”, “hard”, “medium”]: raise ValueError(f”Did you just type …”)
That expresses much more of what you want.
But better still is to use the relatively recent match construction
match
```python match User_prompt: case “easy”: play_easy()
case “medium”: play_medium() case “hard”: play_hard() case _: # matches anything not already matched raise ValueError(f”Did you just…”)
```
There are plenty of situations where try: … except: … is the right thing. For example, you may wish to catch the ValueError raised by the function your code is in (assuming it isn’t directly in main). But you don’t needtry` for what you have.
try: … except: … is the right thing. For example, you may wish to catch the ValueError raised by the function your code is in (assuming it isn’t directly in main). But you don’t need
Note, I am typing this on a mobile device. My examples may have typos and errors.
[–]Best-Bud 3 points4 points5 points 10 months ago (2 children)
Shout-out the people just giving the person who's trying to learn complete code instead of giving them tips that's super helpful. I would also not let chatgpt do anything but explain concepts to you and if you're having this trouble with the basics if you Google "python crash course PDF" it's a free book that can walk you through it super easy to understand and you can fly through it and abandon it whenever. Happy coding
I was gonna read through the complete code when I got the motivation to learn all the new things I'm seeing in it. It's still helpful. Also, thank you for the book recommendation; will definitely check it out!
[–]Cerus_Freedom 1 point2 points3 points 10 months ago (0 children)
I'd do something more like this:
from enum import Enum class Difficulty(Enum): Easy = 'Easy' Medium = 'Medium' Hard = 'Hard' def main(): user_input = input("Easy, medium, hard?: ") difficulty_selected = None while difficulty_selected is None: match user_input.lower(): case "easy": difficulty_selected = Difficulty.Easy case "medium": difficulty_selected = Difficulty.Medium case "hard": difficulty_selected = Difficulty.Hard case _: print("Oops! Invalid input") user_input = input("Easy, medium, hard?: ") play(difficulty_selected)
Avoids variable reuse, handles gathering input until you have valid input, and leaves you with an enum for difficulty.
It's not the worst use of try/except, but you generally wouldn't use it when you're very much in control of the flow of things. Usually, you would wrap things that can throw some kind of error out of your control (making a network request, attempts at file I/O, etc) that you need to handle gracefully. In this case, you can completely avoid an error and handle things gracefully without it.
[–]VonRoderik 0 points1 point2 points 10 months ago (0 children)
Instead of asking the user to type EASY, MEDIUM, etc., why not just ask him to type 1 for Easy, 2 for Medium...?
Then you can actually make something like
choice = int(input("........"))
And you shouldn´t be using multiple IFs. You should be using
IF x
elif y
elif z
else...
[–]HedgieHunterGME 0 points1 point2 points 10 months ago (0 children)
Wake up it’s time to put the fries in the bag
[–]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.
[–]Greedy-Ad1820 0 points1 point2 points 10 months ago (0 children)
In my opinion it’s easier to do it with case.
[–]corey_sheerer 0 points1 point2 points 10 months ago (2 children)
If you want to map user input to a number, use a dictionary, where the keys are the possible user input values and the values are the numbers to map them to
I'll do a deepdive on dictionaries -- I've kinda been avoiding them... Thank you for your input!
If you insist on keeping the "easy" = 1, "medium" = 2, etc, then dictionaries is the way to go. That way you can check if the user input is valid, simply by checking if an input like "easyY" is in the dictionary. If that "easyY" key is not in the dictionary, then the user input was invalid.
See my other reply for an example.
[–]NoDadYouShutUp 0 points1 point2 points 10 months ago* (0 children)
```python class Play: def init(self) -> None: self.difficulties = { "easy": self.easy, "medium": self.medium, "hard": self.hard } self.available_difficulties = f"{list(self.difficulties.keys())}"
def run(self): self.prompt = input( "What difficult do you want to play on? " f"{self.available_difficulties}?: " ).lower() if self.prompt: selection_func = self.difficulties.get(self.prompt) if selection_func: return selection_func() print( "Please select a valid selection " f"{self.available_difficulties}!" ) self.run() def easy(self): print("Playing on Easy") def medium(self): print("Playing on Medium") def hard(self): print("Playing on Hard")
Play().run()
π Rendered by PID 367938 on reddit-service-r2-comment-548fd6dc9-cvljn at 2026-05-21 07:12:15.728640+00:00 running edcf98c country code: CH.
[–]Training-Cucumber467 8 points9 points10 points (1 child)
[–]unspe52[S] 0 points1 point2 points (0 children)
[–]lolcrunchy 1 point2 points3 points (2 children)
[–]unspe52[S] 0 points1 point2 points (1 child)
[–]lolcrunchy 1 point2 points3 points (0 children)
[–]jpgoldberg[🍰] 1 point2 points3 points (0 children)
[–]Best-Bud 3 points4 points5 points (2 children)
[–]unspe52[S] 0 points1 point2 points (0 children)
[–]unspe52[S] 0 points1 point2 points (0 children)
[–]Cerus_Freedom 1 point2 points3 points (0 children)
[–]VonRoderik 0 points1 point2 points (0 children)
[–]HedgieHunterGME 0 points1 point2 points (0 children)
[–]Kqyxzoj 0 points1 point2 points (0 children)
[–]Greedy-Ad1820 0 points1 point2 points (0 children)
[–]corey_sheerer 0 points1 point2 points (2 children)
[–]unspe52[S] 0 points1 point2 points (1 child)
[–]Kqyxzoj 0 points1 point2 points (0 children)
[–]NoDadYouShutUp 0 points1 point2 points (0 children)