+ 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!
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.
+ 4
Apologies. Crossed wires ☺
+ 3
It was the int cast too that helped.
+ 3
Yes, I know. I'm showing you that with the print() to explain your problem.
+ 3
Shall I change that to working code?
+ 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!
+ 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?
+ 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 ☺
+ 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)
+ 2
Use the original code I posted, not that copy.
+ 2
Jackson Meddows You're still using the input() function incorrectly.
+ 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...
+ 1
That code you linked me to returns a TypeError
0
OH MY GOD I HAD TO MAKE A FLOAT ARE YOU KIDDING???? UGH
thanks
0
How do I make it not print the five zeros at the beginning??
0
Oh I see. Somehow I misinterpreted what you were trying to say.
0
yeah go ahead please
0
Why does your code work while mine doesn't?
0
I fixed it. If you take a look at the current code, I’m satisfied with the result