+ 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
16 Respostas
+ 2
Оксана Кувыркова no problem mate, just try to be more precise next time :)
+ 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 ?
+ 1
𝕵𝖆𝖓 𝕸𝖆𝖗𝖐𝖚𝖘 lol thanks for sharing the solution
+ 1
Jan Markus, thank you for visual explanation. It was exact
+ 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
+ 1
I need to admit my question was really stupid
+ 1
𝕵𝖆𝖓 𝕸𝖆𝖗𝖐𝖚𝖘 i guess it was enough liquid already, when i've been crying on this code befor came here to ask)
+ 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.
+ 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.
+ 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
+ 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))
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.
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])
0
𝕵𝖆𝖓 𝕸𝖆𝖗𝖐𝖚𝖘 😂😂
0
Оксана Кувыркова then why did you give the confusing examples with lists and integers ? Please know how to format your question well next time
0
𝕵𝖆𝖓 𝕸𝖆𝖗𝖐𝖚𝖘 her struggle
😂