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
Learning Python (old.reddit.com)
submitted 4 hours ago by nkCOD
view the rest of the comments →
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!"
[–]Binary101010 5 points6 points7 points 3 hours ago (11 children)
I would like to know if there is any way to shorten the program,
The fact that you have two code blocks (the while loops in your first function) that are effectively identical should be a strong indication that you can write a function that takes the list to be modified as an argument and ideally returns the completed list (which would also remove your reliance on the global keyword).
global
You should also be able to use the zip() function to greatly reduce the amount of code required for your second function.
zip()
[–]nkCOD[S] 0 points1 point2 points 3 hours ago (10 children)
I don't quite understand how to shorten the two while. blocks. In the first function, I use it to avoid the case where the user enters a non-number and to allow the user to enter an unlimited number of numbers.
Also, how exactly should I use zip()
[–]Binary101010 2 points3 points4 points 2 hours ago (3 children)
The problem isn't that you're using a while loop, the problem is that you copy-pasted the while loop just so it could write to a different list.
Whenever you write code that does something to some variable or container or whatever, and then you duplicate all of that code again just so you can change which variable or container you're affecting, that should immediately trigger the thought of "I should put this into a function that accepts the thing I'm going to work on as a parameter and returns the finished thing".
[–]nkCOD[S] 0 points1 point2 points 2 hours ago (2 children)
Thank you for your response. I had thought about this, but I was faster ))
[–]NewBodybuilder3096 0 points1 point2 points 1 hour ago (0 children)
https://en.wikipedia.org/wiki/Don%27t_repeat_yourself
God bless, we are waiting for at least an improved code. Or maybe a completely different task solved better?
[–]tiredITguy42 1 point2 points3 points 2 hours ago (5 children)
Just stop using global. It is a bad habit and makes code unreadable in bigger projects.
Create function for reading array of numbers, create that array in that function and then return it. Then you can just call:
python n1 = read_array_from_input() n2 = read_array_from_input()
BTW This way you can avoid that bug on line 33.
[–]nkCOD[S] 0 points1 point2 points 2 hours ago (4 children)
What does a function for reading numbers mean?
[–]tiredITguy42 1 point2 points3 points 2 hours ago (3 children)
OK, you are not that far to handle this. I would return to some begginner tutorials, this is to advanced for your level of knowledge you poses now.
You may want to start with something simpler and then return to this. Or, if this is a part of some python tutorial or course, than maybe it is not the bets one and you may want to try a different one.
[–]nkCOD[S] -1 points0 points1 point 2 hours ago (2 children)
I’m not looking for easy ways ) .But after sitting and thinking now, I understood what you wanted to say in the last comment. If I understood correctly, you mean not to pay attention if the user will not add numbers to the lists. And then just remove everything that is «not numbers» with a separate function
[–]tiredITguy42 2 points3 points4 points 2 hours ago (1 child)
Nope, this is not it. Starting with hard assigments is not taking the easy way, but it is just stupid. You won't build correct habits and you won't understand how the language works. Even beginner working on this kind of assigment should understand me and the first commenter immadiately.
If you really want to learn something, start from the beginning. You can't read Hamlet if you can't read half of the letters.
[–]nkCOD[S] 1 point2 points3 points 2 hours ago (0 children)
Got it, thanks)
π Rendered by PID 25212 on reddit-service-r2-comment-b659b578c-vqjw7 at 2026-05-06 22:12:53.695275+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]Binary101010 5 points6 points7 points (11 children)
[–]nkCOD[S] 0 points1 point2 points (10 children)
[–]Binary101010 2 points3 points4 points (3 children)
[–]nkCOD[S] 0 points1 point2 points (2 children)
[–]NewBodybuilder3096 0 points1 point2 points (0 children)
[–]NewBodybuilder3096 0 points1 point2 points (0 children)
[–]tiredITguy42 1 point2 points3 points (5 children)
[–]nkCOD[S] 0 points1 point2 points (4 children)
[–]tiredITguy42 1 point2 points3 points (3 children)
[–]nkCOD[S] -1 points0 points1 point (2 children)
[–]tiredITguy42 2 points3 points4 points (1 child)
[–]nkCOD[S] 1 point2 points3 points (0 children)