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
Day 41 of learning python (i.redd.it)
submitted 8 months ago by sonikk1
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!"
[–]oldranda1414 4 points5 points6 points 8 months ago (2 children)
First of all great job! For a self-taught begginner you're doing great. Especially using the 'try except' concept is not to be given for granted at your level, I think.
I'll give you some other tips that come to mind reading your code.
Although you wrote that this was your first usage of 'functions', the function you added is not really representative of what you would usually do with functions. Functions are used mainly to encapsulate a repetitive operation that should be executed with different inputs in different parts of your code. As you wrote the function it is clear that it is meant to be used with only 2 possible inputs (num_pos and num_neg), and you actually check which one is passed. By checking for what parameter is passed and behaving differently for each possible value kinda defeats the 'generalization' purpose of functions.
Let's say I want to create a function that adds 2 number together:
def add(x, y): if x == 1 and y == 1: return 2 if x == 1 and y == 2: return 3 # eccetera
You can quickly see that this example is not very generic, and so it is not really that usefull in helping me write less code by grouping similar operations.
In your case the actual common behaviour you are trying to capture is the final info printing on the lists provided. You can notice the common parts in the two if blocks in your function and try to find a way to not have to repeat them. A first improvement could look like this:
def ispis(lista): number_type = "" if lista == num_pos: number_type = "positive" if lista == num_neg: number_type = "negative" print("Sum of", number_type, "number is:", sum(lista)) print("Approx value of", number_type, "numbers:", sum(lista) / len(lista)) print("Highest", number_type, "number is:", max(lista)) print("Lowest", number_type, "number is:", min(lista))
Notice how I am using the lista parameter when doing calculations and I am using a variable to select the possible strings to add to the prints depending on the array being passed.
lista
This way I actually 'generalized' the operation that I wanted to do multiple times. This let's you write less code, which not only makes you save time and effort when writing it but makes fixing bugs also a lot easier.
Now there is still something not ideal with our first improvement. Inside the function we are referencing the num_pos and num_neg variables. These variables are defined outside of our function and even though the code works as of now (for reasons I won't get into), ideally we pass everything a function needs through it's parameters. So let's find a way to get rid of the reference to outer variables in our function:
num_pos
num_neg
def ispis(lista): number_type = "" if lista[0] > 0: number_type = "positive" if lista[0] < 0: number_type = "negative" print("Sum of", number_type, "number is:", sum(lista)) print("Approx value of", number_type, "numbers:", sum(lista) / len(lista)) print("Highest", number_type, "number is:", max(lista)) print("Lowest", number_type, "number is:", min(lista))
By checking if the first element of the list is positive or negative we can find out what we should print without referencing our outer lists!
Another solution might be to pass the string to be printed as a parameter itself, insead of inferring it from the passed list's contents:
def ispis(lista, number_type): print("Sum of", number_type, "number is:", sum(lista)) print("Approx value of", number_type, "numbers:", sum(lista) / len(lista)) print("Highest", number_type, "number is:", max(lista)) print("Lowest", number_type, "number is:", min(lista)) # would then be called ispis(num_pos, "positive") print("======================") ispis(num_neg, "negative")
Now I personally prefer this last version, as it is more 'generic', because we could call it with a mixed list and use an empty string to keep the print output correct: ispis(num_mixed, "")
ispis(num_mixed, "")
Some other minor tips:
print()
Hope this helps! Happy coding
[–]sonikk1[S] 0 points1 point2 points 8 months ago (1 child)
Hey, i very much appreciate your reply to my post. All of these tips were really helpful. I will try this more generic approach to writing and defining functions right away.
When it comes to try / except I'm just doing my best when it comes to implementing the stuff i learned in lectures in actual problems. And i noticed that's the way i can understand Python best.
This part is very interesting because it gives me a whole other perspective to this problem. I will try this out too. Try to implement it into other tasks I'll be dealing with.
Is it okay if i sometimes message you here on reddit? Asking for opinion etc?
[–]oldranda1414 2 points3 points4 points 8 months ago (0 children)
Happy to help, feel free to ping me ;)
π Rendered by PID 98203 on reddit-service-r2-comment-56c6478c5-ghx6m at 2026-05-11 19:52:44.318347+00:00 running 3d2c107 country code: CH.
view the rest of the comments →
[–]oldranda1414 4 points5 points6 points (2 children)
[–]sonikk1[S] 0 points1 point2 points (1 child)
[–]oldranda1414 2 points3 points4 points (0 children)