+ 1

FIX MY CODE PLEASE

https://code.sololearn.com/cTZoRPYB5IA2 I'm trying to get the program to add up five numbers from user input and then find their average by dividing them by five, but it's not working. Please help!

23rd Apr 2018, 10:28 PM
Jackson Meddows
Jackson Meddows - avatar
19 Antworten
+ 4
The input() gets strings by default. So the data1 + etc, is string concatenation and not integer arithmetic. https://code.sololearn.com/cc31a1J68C8x/?ref=app Later this means you are dividing a string by 5, hence the error message of the python interpreter.
23rd Apr 2018, 10:38 PM
Emma
+ 4
Apologies. Crossed wires ☺
23rd Apr 2018, 10:43 PM
Emma
+ 3
It was the int cast too that helped.
23rd Apr 2018, 10:33 PM
Emma
+ 3
Yes, I know. I'm showing you that with the print() to explain your problem.
23rd Apr 2018, 10:41 PM
Emma
+ 3
Shall I change that to working code?
23rd Apr 2018, 10:43 PM
Emma
+ 3
Sorted out. I'm new to python, so it took me a while. The argument of input() is a prompt, which won't show up in the SoloLearn Code Playground! That's what the five zeroes were!
23rd Apr 2018, 10:59 PM
Emma
+ 3
You initialised data1, data2 etc to 0. You then called input(data1) etc. But the argument of input() is a prompt to the user (i.e. the prompt is printed out). You were printing a zero out each time (the five zeroes all in a row). I eventually cast the inputs to float, as this will allow user input 4 or 4.3 Does that make sense?
23rd Apr 2018, 11:12 PM
Emma
+ 3
The phantom downvoter strikes again. Sometimes on SoloLearn, somebody like myself helps another person, and a mindless person downvotes, good answers and good will. Pathetic behaviour, but I guess it gives them something to feel good about 😂 I just upvoted everything back to zero ☺
24th Apr 2018, 9:41 AM
Emma
+ 2
print("Please input five data points on separate lines: ") data1 = 0 data2 = 0 data3 = 0 data4 = 0 data5 = 0 data1 = int(input(data1)) data2 = int(input(data2)) data3 = int(input(data3)) data4 = int(input(data4)) data5 = int(input(data5)) sum = (data1 + data2 + data3 + data4 + data5) n = 5.0 print(sum/n)
23rd Apr 2018, 10:32 PM
Emma
+ 2
Use the original code I posted, not that copy.
23rd Apr 2018, 10:42 PM
Emma
+ 2
Jackson Meddows You're still using the input() function incorrectly.
24th Apr 2018, 9:44 AM
Emma
+ 1
@Xan Yeah I just noticed that you changed that part. Can you please explain why? The section of the course on user input isn't very helpful...
23rd Apr 2018, 10:34 PM
Jackson Meddows
Jackson Meddows - avatar
+ 1
That code you linked me to returns a TypeError
23rd Apr 2018, 10:40 PM
Jackson Meddows
Jackson Meddows - avatar
0
OH MY GOD I HAD TO MAKE A FLOAT ARE YOU KIDDING???? UGH thanks
23rd Apr 2018, 10:32 PM
Jackson Meddows
Jackson Meddows - avatar
0
How do I make it not print the five zeros at the beginning??
23rd Apr 2018, 10:35 PM
Jackson Meddows
Jackson Meddows - avatar
0
Oh I see. Somehow I misinterpreted what you were trying to say.
23rd Apr 2018, 10:42 PM
Jackson Meddows
Jackson Meddows - avatar
0
yeah go ahead please
23rd Apr 2018, 10:44 PM
Jackson Meddows
Jackson Meddows - avatar
0
Why does your code work while mine doesn't?
23rd Apr 2018, 11:03 PM
Jackson Meddows
Jackson Meddows - avatar
0
I fixed it. If you take a look at the current code, I’m satisfied with the result
24th Apr 2018, 1:46 AM
Jackson Meddows
Jackson Meddows - avatar