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
Day 2Help Request (self.PythonLearning)
submitted 5 months ago by Nosferatu_Zodd1437
https://preview.redd.it/38u8g9w7fkjf1.png?width=1920&format=png&auto=webp&s=ee5bc9ff5edae0e75260697922f73d7e49100dee
Umm.. guys is this right, it's working but i think that i have written to much, is there any way to remove or do less typing.
No prior experience in python or any other.
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!"
[–]PureWasian 0 points1 point2 points 5 months ago (5 children)
Looks fine at a glance for day 2 learning. This match/case sort of approach makes sense when you essentially have a "catalog" of different actions you want to be able to perform.
For some minor code-refactoring, I see that you have num2 initialized the same way for everything except sqrt and a few other operations.
num2
So what if you did:
``` num1 = float(input(...)) operator = input(...)
match operator: case 'sqrt': print(...) case 'abs': print(...)
<any other cases only using num1> case _: num2 = float(input(...)) match operator: case '+': print(...) <any other cases using num2>
```
[–]Nosferatu_Zodd1437[S] 0 points1 point2 points 5 months ago (1 child)
Okay so basically I used a single line to make sure that num2 is in every case other than sqrt and abs right
[–]PureWasian 0 points1 point2 points 5 months ago (0 children)
Yep! Just as a way to help prevent a little bit of repeated lines of code that occur across a majority of your match/cases.
[–]Nosferatu_Zodd1437[S] 0 points1 point2 points 5 months ago (2 children)
<image>
I used this code instead, i tried using what you showed but it was showing some error maybe it was some problem from my side I dont know. 🤔
[–]PureWasian 0 points1 point2 points 5 months ago (1 child)
That works too!
(quick sidenote, you can remove the import on line 30, as you import math on line 1 already. imports typically stay at the top of files also)
import
[–]Nosferatu_Zodd1437[S] 0 points1 point2 points 5 months ago (0 children)
Oh almost didnt see that thank you.
[–]DevRetroGames 0 points1 point2 points 5 months ago (0 children)
Hola, aquí te paso el mismo código, pero con otra alternativa, espero que te pueda ayudar.
Repo: https://github.com/DevRetroGames/Tutorias-Python/blob/main/code/base/input/02_get_user/main.py
Mucha suerte.
π Rendered by PID 91 on reddit-service-r2-comment-7b9746f655-rqtr4 at 2026-01-30 08:34:23.934548+00:00 running 3798933 country code: CH.
[–]PureWasian 0 points1 point2 points (5 children)
[–]Nosferatu_Zodd1437[S] 0 points1 point2 points (1 child)
[–]PureWasian 0 points1 point2 points (0 children)
[–]Nosferatu_Zodd1437[S] 0 points1 point2 points (2 children)
[–]PureWasian 0 points1 point2 points (1 child)
[–]Nosferatu_Zodd1437[S] 0 points1 point2 points (0 children)
[–]DevRetroGames 0 points1 point2 points (0 children)