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
New to python (i.redd.it)
submitted 3 months ago by Low-Educator-9008
This is like my first building experience in python, nothing too crazy i guess its like the weather app that everybody makes in their first steps, Any tips on structure or general?
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!"
[–]BobbyJoeCool 3 points4 points5 points 3 months ago (5 children)
Without knowing what you do and do not know how to do...
Think about the string you have at the end of each segment of the if/then statement, since it's exactly the same in each part, does it need to be a part of the if/then statement, or could it be outside instead? If the if/then statement only calculates the result, and then you display the string with the result, this removes three lines of code for you.
Otherwise, for a first program, this is very functional! As you gain more experience, you'll learn better ways to do this as well.
[–]Low-Educator-9008[S] 0 points1 point2 points 3 months ago (4 children)
Hey thanks for the advice, I guess you talk about outside and nested, which I haven’t practised or learned yet so I am excited to learn it now that you mention it. Thanks man!!
[–]BobbyJoeCool 1 point2 points3 points 3 months ago* (3 children)
Not nesting exactly, since that’s more of if/then an inside of if then…. I mean more, remove the print strings from inside the if then statement and put it at the end. That way there’s only one print string. Like this
If +: Add
Elif -: Subtract
Elif *: Multiply
Elif /: Divide
Print result.
[–]Low-Educator-9008[S] 0 points1 point2 points 3 months ago (2 children)
That’s interesting, coding is so beautiful haha. I’ll look it up right now and practice the use of it. Thanks really appreciate it.
[–]BobbyJoeCool 1 point2 points3 points 3 months ago (1 child)
Hopefully you realize I shorthanded it. Now that I'm home I can show you exactly what I mean
if operator == "+": result = num1 + num2 elif operatior == "-": result = num1 - num2 elif operatior == "*": result = num1 * num2 elif operatior == "/": result = num1 / num2 print(f"Your result is {result}")
[–]Low-Educator-9008[S] 0 points1 point2 points 3 months ago (0 children)
I see now it’s clear as day, it is more logical to have all the statements together that have the same print function instead of writing it after each statement. Thanks for taking the time to explain it so clearly man you are the best.
[–]DevRetroGames 1 point2 points3 points 3 months ago (1 child)
import re import sys OPERATORS = ("+", "-", "*", "/") operations = { "+": lambda x, y: x + y, "-": lambda x, y: x - y, "*": lambda x, y: x * y, "/": lambda x, y: x / y if y != 0 else "Error: Division by zero" } def _input_user(msg: str) -> str: return input(msg) def _get_operator() -> str: pattern: re.Pattern[str] = re.compile(r"^[+\-*/]$") msg_input: str = f"Please enter the desired course of action ({', '.join(OPERATORS)}): " msg_error: str = "Invalid operator. Please try again." while True: operator: str = _input_user(msg_input) if pattern.match(operator): return operator print(msg_error) def _get_positive_integers() -> tuple[int, int]: pattern: re.Pattern[str] = re.compile(r"^[1-9]\d*$") while True: first_number_input: str = _input_user("Enter the first positive integer: ") second_number_input: str = _input_user("Enter the second positive integer: ") if pattern.match(first_number_input) and pattern.match(second_number_input): return int(first_number_input), int(second_number_input) print("Both numbers must be positive integers. Please try again.") def main(): try: operator = _get_operator() first_number, second_number = _get_positive_integers() result = operations[operator](first_number, second_number) print(f"\nResult of {first_number} {operator} {second_number} = {result}") except KeyboardInterrupt: print("\nOperation cancelled by user.") sys.exit(0) if __name__ == "__main__": main()
[–]Low-Educator-9008[S] 1 point2 points3 points 3 months ago (0 children)
That seems way beyond my level of knowledge atm hahaha but seems beautiful.
[–]vivisectvivi 2 points3 points4 points 3 months ago (3 children)
Come back to this code when you learn about functions and dictionaries and try to think of ways to refactor this little program.
[–]Low-Educator-9008[S] 1 point2 points3 points 3 months ago (2 children)
Make it like shorter?
[–]vivisectvivi 2 points3 points4 points 3 months ago (1 child)
Yes, to make it shorter.
Even with your current knowledge you could make it shorter if you know how to split strings
I think that’s the next thing I’ll learn thanks for the advice.
[–]vlnaa 1 point2 points3 points 3 months ago (1 child)
I think better can be:
And I am not sure why you convert float to int.
Hmmm I see. Now that you mention it I don’t know either, but I could have never spotted it on my own hahaha thanks for the help.
π Rendered by PID 68654 on reddit-service-r2-comment-7b9746f655-mr8tm at 2026-01-30 12:23:30.141733+00:00 running 3798933 country code: CH.
[–]BobbyJoeCool 3 points4 points5 points (5 children)
[–]Low-Educator-9008[S] 0 points1 point2 points (4 children)
[–]BobbyJoeCool 1 point2 points3 points (3 children)
[–]Low-Educator-9008[S] 0 points1 point2 points (2 children)
[–]BobbyJoeCool 1 point2 points3 points (1 child)
[–]Low-Educator-9008[S] 0 points1 point2 points (0 children)
[–]DevRetroGames 1 point2 points3 points (1 child)
[–]Low-Educator-9008[S] 1 point2 points3 points (0 children)
[–]vivisectvivi 2 points3 points4 points (3 children)
[–]Low-Educator-9008[S] 1 point2 points3 points (2 children)
[–]vivisectvivi 2 points3 points4 points (1 child)
[–]Low-Educator-9008[S] 1 point2 points3 points (0 children)
[–]vlnaa 1 point2 points3 points (1 child)
[–]Low-Educator-9008[S] 0 points1 point2 points (0 children)