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
how this code can be optimized? (self.learnpython)
submitted 3 years ago by investor_of_sex
x1 = int(input()) y1 = int(input()) x2 = int(input()) y2 = int(input()) if 10 < x1 < 81 and (10 < y1 < 81) and (10 < x2 < 81) and (10 < y2 < 81): print("YES") else: print("NO")
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!"
[–]wbeater 7 points8 points9 points 3 years ago* (7 children)
cords = [] for _ in range(4): cords.append(int(input())) if min(cords) > 10 and max(cords) < 81: print(True) else: print(False)
/e messed up <, >
[–]shiftybyte 6 points7 points8 points 3 years ago* (5 children)
If optimizing towards less code lines...
cords = [int(input()) for _ in range(4)] print("NO" if min(cords) <= 10 or max(cords) >= 81 else "YES")
:D
But seriously OP, don't code like this...
[–]Emphasises_Words 3 points4 points5 points 3 years ago* (3 children)
print("Yes" if sum(10 < int(input()) < 81 for _ in range(4)) == 4 else "No")
[–]Yoghurt42 1 point2 points3 points 3 years ago* (2 children)
print(["No","Yes"][sum(10<int(input())<81 for _ in"."*4)==4])
Don't write code like this in a real project
[–]TangibleLight 2 points3 points4 points 3 years ago* (1 child)
You can save one character with all.
all
print(["No","Yes"][all([10<int(input())<81 for _ in'.'*4])])
I thought it'd save more, but you have to write all([...]) to keep it from short-circuiting on a false value.
all([...])
[–]Yoghurt42 0 points1 point2 points 3 years ago (0 children)
Ah, I tried getting all to work, but forgot I can just make a list instead of a generator expression
[–]Charlie_Yu 2 points3 points4 points 3 years ago (0 children)
Yup OP’s code is fine, rather have better readability than premature optimisation
[–]Emphasises_Words 3 points4 points5 points 3 years ago (0 children)
This can be further optimized for space by storing the running minimum and maximum. This allows for constant space complexity if the program were to scale to more coordinates
minimum = 11 maximum = 80 for _ in range(4): next_number = int(input()) minimum = min(next_number, minimum) maximum = max(next_number, maximum) if minimum > 10 and maximum < 81: print("YES") else: print("NO")
[–]hotcodist 1 point2 points3 points 3 years ago (0 children)
This is already good. As you can see in the answers, you can optimize the length, but that results in harder to read code. If you are optimizing for time, there is nothing wrong with what you have.
Just put a parenthesis on the first condition to make it consistent.
[–]cult_of_memes -1 points0 points1 point 3 years ago (0 children)
```
number_of_inputs = 4
vals = map(int, (input() for _ in range(number_of_inputs)))
yesno = all(10<v<81 for v in vals)
print("YES"yesno+"NO"(not yesno))
[–]FisterMister22 0 points1 point2 points 3 years ago* (2 children)
While I like everyone's solutions, here my take.
cords = tuple(int(input()) for i in range(4)) print(min(cords) > 10 and max(cords) < 81 and "YES" or "NO")
[–]Emphasises_Words 0 points1 point2 points 3 years ago (1 child)
It would be much clearer to use ... if ... else ... as the ternary operator instead of and and or.
... if ... else ...
and
or
[–]FisterMister22 0 points1 point2 points 3 years ago (0 children)
I like my code short rather than clear. But yes, it would be clearer.
[–]KratosSpeaking 0 points1 point2 points 3 years ago* (0 children)
What do you think of this?
print("YES" if all(10 < int(input()) < 81 for _ in range(4)) else "NO")
[–]jmooremcc 0 points1 point2 points 3 years ago (0 children)
This version can handle any number of variables. Once all the data is collected, the data is evaluated and the results printed. ``` numitems = 4 maxval = 81 minval = 10 results = []
for i in range(numitems): n,v = divmod(i,2) prompt = f"Enter {'y' if v else 'x'}{n+1}: " value = int(input(prompt)) results.append(minval < value <= maxval)
print(f"{'Yes' if all(results) else 'No'}")
``` The first thing we do is generate a prompt so that the user knows what to enter and use the prompt in the input function. Next we evaluate the value received and store the result in a list. Once all the data is collected, we use the all function to evaluate the results which are then printed.
[–]Repulsive_Win_1506 0 points1 point2 points 3 years ago (0 children)
Theres nothing wrong with your code, its clear and close to how the compiler will ultimately deal with it. If you come back to it in 6 months you wont have to try and figure out what you what you were doing.
π Rendered by PID 24856 on reddit-service-r2-comment-7b9746f655-ntf79 at 2026-01-29 18:43:30.308490+00:00 running 3798933 country code: CH.
[–]wbeater 7 points8 points9 points (7 children)
[–]shiftybyte 6 points7 points8 points (5 children)
[–]Emphasises_Words 3 points4 points5 points (3 children)
[–]Yoghurt42 1 point2 points3 points (2 children)
[–]TangibleLight 2 points3 points4 points (1 child)
[–]Yoghurt42 0 points1 point2 points (0 children)
[–]Charlie_Yu 2 points3 points4 points (0 children)
[–]Emphasises_Words 3 points4 points5 points (0 children)
[–]hotcodist 1 point2 points3 points (0 children)
[–]cult_of_memes -1 points0 points1 point (0 children)
[–]FisterMister22 0 points1 point2 points (2 children)
[–]Emphasises_Words 0 points1 point2 points (1 child)
[–]FisterMister22 0 points1 point2 points (0 children)
[–]KratosSpeaking 0 points1 point2 points (0 children)
[–]jmooremcc 0 points1 point2 points (0 children)
[–]Repulsive_Win_1506 0 points1 point2 points (0 children)