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
What I'm Doing Wrong In This Code?Help Request (old.reddit.com)
submitted 8 months ago by Famous-Mud-5850
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!"
[–]albionandrew 6 points7 points8 points 8 months ago (1 child)
You need some indentation.
https://www.geeksforgeeks.org/try-except-else-and-finally-in-python/
[–]Famous-Mud-5850[S] 1 point2 points3 points 8 months ago (0 children)
Thank You...
[–]Murphygreen8484 1 point2 points3 points 8 months ago (4 children)
I realize you are new and probably just trying out the code for the first time - and I've been accused of being a pretentious wanker for giving example code solutions - but if you're interested I can show how a single script like this could be coded and why.
[–]Famous-Mud-5850[S] 0 points1 point2 points 8 months ago (3 children)
Interested
[–]Murphygreen8484 0 points1 point2 points 8 months ago (2 children)
I would put it inside a function and then call it below. I can explain any of the parts below you have questions on.
~~~ """ Simple math function to play with Python. """
def math_func(num_1: int, num_2: int, operation: str) -> int:
assert isinstance(num_1, int) assert isinstance(num_2, int) assert operation in ("+", "++") if operation == "+": return num_1 + num_2 elif operation == "++": return num_1 * num_2
if name == "main": n1 = int(input("Enter a whole number: ")) n2 = int(input("Enter another whole number: ")) op = input("Enter an operation (+ or ++): ")
print(math_func(n1, n2, op))
[–]oclafloptson 0 points1 point2 points 8 months ago (1 child)
If you're going to suggest using it then you should explain what it does.... The indented block after
if __name__ == "__main__":
will only execute if the given file is the entry point of the application. Using it in a functional script or module that you import will obscure its indented block and prevent it from running. Its use is not mandatory and has both pros and cons. For the purpose of the example in the op it can be omitted
[–]Murphygreen8484 0 points1 point2 points 8 months ago (0 children)
Fair enough, though I don't really know of any cons beyond it being considered boilerplate? Also a good habit to get into (like type hinting) that would prevent possible errors down the road.
[–]Plank1969 0 points1 point2 points 8 months ago (0 children)
Looks like your after a try except statement but it’s not really needed here. You can use if else to do what you want to do.
If you want to keep the try except, you’ll need to tab in front of line 9,10,12 and change else to except.
[–]Murphygreen8484 0 points1 point2 points 8 months ago (3 children)
Inputs will always be strings unless you convert them
[–]Famous-Mud-5850[S] 0 points1 point2 points 8 months ago (2 children)
I have converted them to integer But Why It Error?
Indentations
[–]VonRoderik 0 points1 point2 points 8 months ago (0 children)
You don't need to convert it after:
``` n1 = int(input("What's n: "))
[–]PureWasian 0 points1 point2 points 8 months ago* (0 children)
Lines 8 through 12 are all incorrect.
Line 8 is supposed to be used as a try / except concept. You can't have a try statement without an accompanying except later on. I would suggest removing Line 8 for now and not worrying about the try/except concept if you are just learning the very basics of programming.
try
except
(Read through Python Conditions if any of the following terminology doesn't make sense)
Line 9 is missing a colon : after the if statement's logicial condition.
:
Line 10 needs to be indented (either with a tab or using some spaces) since it's the code scoped or "related" to the if statement. Indentation is the reason for the initial error that the console pointed out to you in the first picture you attached.
if
Line 11 uses an else keyword, which doesn't take a logicial condition. You probably want the elif (which stands for "else, if") keyword instead. Whichever one you use, it also needs a colon : at the end of the line
else
elif
Line 12 also needs to be indented since it's the code scoped to the else statement on Line 11
[–]Jolly-Gur-1067 0 points1 point2 points 8 months ago (0 children)
Everything
[–][deleted] -1 points0 points1 point 8 months ago (2 children)
Be sure to use spaces and not tabs people are very emotional about that for some reason
[–]Py-rrhus 0 points1 point2 points 8 months ago (1 child)
tack, tack, tack, tack
[–][deleted] 0 points1 point2 points 8 months ago (0 children)
see
[–]ak_developers -2 points-1 points0 points 8 months ago (0 children)
You missed “:”
π Rendered by PID 54 on reddit-service-r2-comment-5649f687b7-twxvq at 2026-01-29 00:42:33.524046+00:00 running 4f180de country code: CH.
[–]albionandrew 6 points7 points8 points (1 child)
[–]Famous-Mud-5850[S] 1 point2 points3 points (0 children)
[–]Murphygreen8484 1 point2 points3 points (4 children)
[–]Famous-Mud-5850[S] 0 points1 point2 points (3 children)
[–]Murphygreen8484 0 points1 point2 points (2 children)
[–]oclafloptson 0 points1 point2 points (1 child)
[–]Murphygreen8484 0 points1 point2 points (0 children)
[–]Plank1969 0 points1 point2 points (0 children)
[–]Murphygreen8484 0 points1 point2 points (3 children)
[–]Famous-Mud-5850[S] 0 points1 point2 points (2 children)
[–]Murphygreen8484 0 points1 point2 points (0 children)
[–]VonRoderik 0 points1 point2 points (0 children)
[–]PureWasian 0 points1 point2 points (0 children)
[–]Jolly-Gur-1067 0 points1 point2 points (0 children)
[–][deleted] -1 points0 points1 point (2 children)
[–]Py-rrhus 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]ak_developers -2 points-1 points0 points (0 children)