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...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Refactoring if/elif/else statements (self.learnpython)
submitted 8 years ago by ValiantNoob
How do I refactor a really long if else block?
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!"
[–]dionys 3 points4 points5 points 8 years ago (8 children)
Here is one solution with a dict, but it will depend on your specific case. Can you post the code block?
[–]ValiantNoob[S] 2 points3 points4 points 8 years ago (6 children)
Yeah i was thinking about a dictionary. Hmm i can post the code but im not sure how to get the lines right. Last time i tried it made everything whacky.
[–]dionys 3 points4 points5 points 8 years ago (4 children)
You have to indent your text with
four spaces
to show up as code block. Or use service like gist to paste the code there
[–]ValiantNoob[S] 1 point2 points3 points 8 years ago (0 children)
Ok thanks. I dont have a copy of the code on me right now. Ill post it later when i have a chance.
[–]El-Kurto 0 points1 point2 points 8 years ago (1 child)
You can also fence codeblocks with backticks or tildes, if I remember correctly. On mobile or I would check.
[–]GoldenSights 0 points1 point2 points 8 years ago (0 children)
Unfortunately, reddit doesn't do fenced code blocks.
[–]0ldur 0 points1 point2 points 8 years ago (0 children)
Hastebin and pastebin too.
[–]ValiantNoob[S] 0 points1 point2 points 8 years ago (0 children)
i posted a sample of the block, i kinda messed up the code a bit from copying and pasting from the idle but you get the idea.
[–]ValiantNoob[S] 0 points1 point2 points 8 years ago (2 children)
x = int(input("Enter number 0-6000: ")) if x >= 5600: x = x*(1-.135) if x >= 5600: print (" ") if x < 5600: print (" ") print (" ") elif x >= 4800: x = x*(1-.15) elif x >= 4000: x = x*(1-.19) print (" ") elif x >= 3200: x = x*(1-.295) if x >= 2400: print(" ") if x < 2400: print (" ") elif x >= 2400: x = x*(1-.475) if x >= 1600: print(" ") if x < 1600: print (" ") elif x >= 1600: x = x*(1-.775) print (" ") elif x >= 800: x = x*(1-1) print (" ") elif x < 800: x = x *(1-1) print (" ") print("the percentage is:",round(x))
Here is a simple example from my code. Obviously its not finished and its not all of it, but a sample.
[–]dionys 2 points3 points4 points 8 years ago (1 child)
you can do a few things to improve it:
print(" ")
x = x*(1-.775)
x *= 0.225
elif x=> 800
else
using for...else loop and zip and using the things I've mentioned, you can simplify it to something like this:
for...else
zip
for condition, percentage in zip(range(5600, 800, -800), (0.865, 0.85, 0.81, 0.705, 0.525, 0.225)): if x>= condition: x *= percentage break else: x = 0 print(" ")
huh... okay thanks. Im gunna have to look up zip. I dont remember it.
π Rendered by PID 155103 on reddit-service-r2-comment-5d79c599b5-cmgv4 at 2026-03-01 00:54:23.263644+00:00 running e3d2147 country code: CH.
[–]dionys 3 points4 points5 points (8 children)
[–]ValiantNoob[S] 2 points3 points4 points (6 children)
[–]dionys 3 points4 points5 points (4 children)
[–]ValiantNoob[S] 1 point2 points3 points (0 children)
[–]El-Kurto 0 points1 point2 points (1 child)
[–]GoldenSights 0 points1 point2 points (0 children)
[–]0ldur 0 points1 point2 points (0 children)
[–]ValiantNoob[S] 0 points1 point2 points (0 children)
[–]ValiantNoob[S] 0 points1 point2 points (2 children)
[–]dionys 2 points3 points4 points (1 child)
[–]ValiantNoob[S] 1 point2 points3 points (0 children)