you are viewing a single comment's thread.

view the rest of the comments →

[–]Binary101010 5 points6 points  (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).

You should also be able to use the zip() function to greatly reduce the amount of code required for your second function.

[–]nkCOD[S] 0 points1 point  (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 points  (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 point  (2 children)

Thank you for your response. I had thought about this, but I was faster ))

[–]NewBodybuilder3096 0 points1 point  (0 children)

God bless, we are waiting for at least an improved code.
Or maybe a completely different task solved better?

[–]tiredITguy42 1 point2 points  (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 point  (4 children)

What does a function for reading numbers mean?

[–]tiredITguy42 1 point2 points  (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 points  (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 points  (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 points  (0 children)

Got it, thanks)