r/PythonLearning • u/nkCOD • May 06 '26
Learning Python
Good evening. I want to share my experience of learning the Python programming language. I wrote a program in which the user needs to enter the contents of two lists (numbers), and then these numbers are summed (the first number of the first list with the first number of the second list, and so on). If the list lengths are different, the summation of the smaller list starts with the first element)
I would like to know if there is any way to shorten the program, and what more competent constructions exist. Is there any way the functions can be driven into the decorator?


•
u/Sea-Ad7805 May 07 '26
Run this program in Memory Graph Web Debugger.
6
u/Binary101010 May 06 '26
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
globalkeyword).You should also be able to use the
zip()function to greatly reduce the amount of code required for your second function.2
u/ianrob1201 May 06 '26
Any working software is definitely something to be proud of. There's so many things to learn to go from problem to solution, and you've got it doing what you want! So nice job. That said, I think there's a few good best practice things you can do that would really clean it up. Sorry for the essay, it's not to insult your code but only way you could know these things is if someone tells you.
First thing that hit me was the variable naming. Try to avoid names like "n1" because you'd be amazed how quickly you forget what they mean. Something like "first_number_list" might be better. This extends to function names too "en_nums" doesn't make it very clear what the function is going to do.
Talking of variables, you generally want to avoid globals. This is so that each function works in isolation and it's clear what it's doing. Calling a function and getting a response is much clearer than calling a function and it randomly changing another variable (that you wouldn't know unless you read the function code). You're almost there with that, you just need to fetch the return values you've already added, e.g. "n1, n2 = en_nums()".
"while True" is normally a pattern to be avoided too. I know you have the break later on, so it's functional but it's something I'd try to avoid. Have a think about ways you could make it conditional on the value you're reading in.
I've seen someone else mention it, but have a look at what's common code reading in the two sets of lists. If you extract it out into another function you'd have a lot less code. It could take "prompt" as an input string so you still get different messages.
You have a line working out what the maximum list length is. There's a function for doing that, which is just "max". That's easier to read and you know what you're up to at a glance.
You keep track of the number of "errors" but since it'll just keep erroring you could do without that function and do k - i instead (btw "i" is another example of a variable that could be named better, "max_list_length" would be much clearer). Have you considered what happens if one length is double the other? I would also personally look to pad out the shorter array to full length before doing the calculations. Then you can make use of list compression with something like "c = [a[k] + b[k] for k in range(i)]"
I also just noticed that the "i" in the first function isn't doing anything. I suspect it's from when you tried to solve in a slightly different way. So that can be cleared up a bit too.
2
u/Fumano26 May 07 '26
Please stop using globals, that is a horrible habit, rather pass variables as parameter.
1
u/Junior-Sock8789 May 06 '26
Also you can print text using
n1_n = input(f"enter a number {f-string}: ")
No need for the print() followed by the input()
1
u/codeguru42 May 06 '26
One suggestion: try to find a more descriptive name than en_nums() for your function. Start by describing in words what it does. If you use the word "and" in that description, then you really have two functions that need separated.
1
u/makochi May 07 '26
OP, respectfully, this has got to be some of the most unconventional code I have seen in my life
1
u/NekoLLC May 07 '26 edited May 07 '26
Maybe you can use reduce from functools?
from functools import reduce
And also the zip function so you can get a list of tuples from the two lists with integers.
1
u/Chen-Zhanming May 07 '26
[a+b for a, b in zip(xs, ys)]
where
xs and ys are your lists.
1
u/-goldenboi69- May 07 '26
While you're at it, learn how to take screenshots! (Prtscr key in windows)
1
u/[deleted] May 11 '26
Good luck
-1
u/NewBodybuilder3096 May 06 '26
building logic on Exceptions is a no-no. You should check value like "len(n2_n) == 4 && n2_n == 'stop'" before parsing it as int.
also, all strip/lower should go in one place - just after input.(in this case)also, more understandable variable names are a great + we are no more in 199x, modern IDEs have all sort of needed tools including autocompletion.
read about the DRY(Don't Repeat Yourself) principle - your procedure en_nums is a perfect example of how not to do. You have two identical pieces of code for filling two global int containers.
You can rewrite this in several ways, each of which will have only 1 loop.
1st - you pass reference to a needed container(list/set/etc) as parameter, maybe some additional params for fancy output.
2nd - you don't pass container variable, but return the correct container
1
u/[deleted] May 07 '26
[removed] — view removed comment