+ 1

Def average

Ok my smart fellows. New troubles. I need method that can take 2 parameters. They can be single numbers or one zero one list, and i need to find average of them. Or average of two numbers or average of list. https://code.sololearn.com/c6ByehO1hu25/?ref=app

16th Dec 2019, 8:52 PM
Оксана Кувыркова
Оксана Кувыркова - avatar
16 Réponses
+ 2
Оксана Кувыркова no problem mate, just try to be more precise next time :)
16th Dec 2019, 9:20 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
Оксана Кувыркова looking at your code, it looks like you completly dumped the input, and reset it to empty lists. This function is easy to make, I will not write the function, but I will help. 1. y should have a default value, because if the inmut is a list, there is no y. 2. Separate cases using if statement: if type(x)==int: # your code here else: # your code jere 3. In line 4 you used len, which will only work on lists, if the input was 3, 5, that would be an error. Note: What is the purpose of having different input types ? Why not makes a function that only takes a list ?
16th Dec 2019, 9:06 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
𝕵𝖆𝖓 𝕸𝖆𝖗𝖐𝖚𝖘 lol thanks for sharing the solution
16th Dec 2019, 9:12 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
Jan Markus, thank you for visual explanation. It was exact
16th Dec 2019, 9:17 PM
Оксана Кувыркова
Оксана Кувыркова - avatar
+ 1
Aynane, thank you forvpushing to think. The problem was in my question in formulation. It was out of my mind that function parameter can take different types. It is simple but i missed it. Now all become clear. Need to work on my questions
16th Dec 2019, 9:19 PM
Оксана Кувыркова
Оксана Кувыркова - avatar
+ 1
I need to admit my question was really stupid
16th Dec 2019, 9:22 PM
Оксана Кувыркова
Оксана Кувыркова - avatar
+ 1
𝕵𝖆𝖓 𝕸𝖆𝖗𝖐𝖚𝖘 i guess it was enough liquid already, when i've been crying on this code befor came here to ask)
16th Dec 2019, 9:28 PM
Оксана Кувыркова
Оксана Кувыркова - avatar
+ 1
𝕵𝖆𝖓 𝕸𝖆𝖗𝖐𝖚𝖘 probably, and that is exactly why I didn't not want to answer. If the question was a bit harder, it wlild be understandable, but for this, she needs to find the solution herself.
16th Dec 2019, 9:29 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
Оксана Кувыркова I can see in your profile that you almost completed the python course, yet struggling with a simple function. I'd recommend you reset the course, or at least go back and read everything you missed, with proper practice.
16th Dec 2019, 9:30 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
Aymane Boukrouh [Unavailable] i reread course time to time, but i have other python courses, in real life, and i become lost on objects and classes, moreover in parallel i learn c# and i am doing better there, but i am practicing more with c# so i become to forget basis of python and receive mix in my had. It is in my to do list on new year hollidays to repeat and train all topics
16th Dec 2019, 10:10 PM
Оксана Кувыркова
Оксана Кувыркова - avatar
+ 1
So.. from you last comment Оксана Кувыркова, you need a function that can find the average if a list or individual elements are passed to it.. here you go.. def average(x, *y): if isinstance(x, (list, tuple)): return sum(x)/len(x) else: return sum(y, x)/(len(y)+1) print(average([40, 20, 20])) print(average(40, 20, 20))
17th Dec 2019, 6:25 PM
rodwynnejones
rodwynnejones - avatar
0
Note: a function and a method are two different things, and it looks that you are writing a function, pay attention next time :) Your post confuses me, can you write more about what you want to achieve ? Try to give example, at least 2 different examples.
16th Dec 2019, 8:57 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
0
Hi Aymane. I need function, i did a mistake in explanation. For example i can heve input average(3, 25) or average([3, 15, 34, 1, 8])
16th Dec 2019, 9:01 PM
Оксана Кувыркова
Оксана Кувыркова - avatar
16th Dec 2019, 9:17 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
0
Оксана Кувыркова then why did you give the confusing examples with lists and integers ? Please know how to format your question well next time
16th Dec 2019, 9:18 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
16th Dec 2019, 9:22 PM
Оксана Кувыркова
Оксана Кувыркова - avatar