Hello, I need help making a for loop in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Hello, I need help making a for loop in python

This is my first time coding. i was tasked to make a for loop that inputs 5 numbers and outputs their total. I tried my best but there seems to be something wrong with the output and it prints "Enter a Number" when it isnt supposed to...what have i done wrong? https://code.sololearn.com/cd2d4JNKq75L/?ref=app

10th May 2023, 4:08 AM
Lemon Kat
Lemon Kat - avatar
26 Answers
+ 2
if you pass an argument to input() (eg input("Enter a Number") ), it uses that argument as a prompt. Unfortunately the sololearn playground doesn't accept input normally, but only all at once at the beginning, so things like that behave strangely here.
10th May 2023, 4:54 AM
Orin Cook
Orin Cook - avatar
+ 2
Don't write anything inside input method and enter inputs like this: 4 5 6 7 8 now enter
10th May 2023, 4:21 AM
A͢J
A͢J - avatar
+ 2
Евгений haha yes i read it. im honestly very happy with the response from the community. although some code went above my head cus im a complete noob it was very cool to see how yall came up with the various solutions. and no it doesnt matter for me if its in the same line or not, just needed the code to work and show the proper output(that is the sum of 5 numbers using for loop). ive never really coded before and this was fun. found out sololearn has a few problems but ig ill get used to it or just learn more once i go to more advanced lessons. again tysm :)
12th May 2023, 3:17 PM
Lemon Kat
Lemon Kat - avatar
+ 1
Have you tried the beginner's python course here?
10th May 2023, 7:40 AM
Евгений
Евгений - avatar
+ 1
ꘘꫀⲅძ᥆͟ᥙ᥉᯽ If there are different inputs then your code will work?
12th May 2023, 3:47 AM
A͢J
A͢J - avatar
+ 1
ꘘꫀⲅძ᥆͟ᥙ᥉᯽ your code reads just one number and multiplies it by 5.
12th May 2023, 8:35 AM
Евгений
Евгений - avatar
+ 1
Yes you are right it won't work for multidigit thanks so we can fix the code to be like this: nums=input().split(" ") num=[ float(i) for i in nums] print(sum(num))
12th May 2023, 9:06 AM
Muhammad Ahmad Rasheed
Muhammad Ahmad Rasheed - avatar
+ 1
No problem we are ejoing and no problem if it solved we just make doscussions and this is my solution if you use multy line thanks for all and welcome. num=[x:=input() for i in range(5)] ct=[float(i) for i in num] print (sum(ct))
12th May 2023, 10:44 AM
Muhammad Ahmad Rasheed
Muhammad Ahmad Rasheed - avatar
+ 1
You don't need `x` in the first line.
12th May 2023, 10:53 AM
Евгений
Евгений - avatar
+ 1
Looks like the last solution is when each number is on separate line, and not mixed (like two on one line, three on the other etc.) Then this could be compacted to this: ``` print(sum(float(input()) for _ in range(5))) ```
12th May 2023, 11:03 AM
Евгений
Евгений - avatar
+ 1
I've come up with the following solution for reading numbers on undefined number of lines with several on each line into a flat list: ``` import sys numbers = [ float(num) for line in sys.stdin.readlines() for num in line.split() ] ``` This even handles empty lines, tabs, double spaces, leading whitespace characters, etc. I've split it into separate lines for readability.
12th May 2023, 11:30 AM
Евгений
Евгений - avatar
0
ah i c, but why did the string in the input prompt become an output?
10th May 2023, 4:27 AM
Lemon Kat
Lemon Kat - avatar
0
ohh, tysm guys
10th May 2023, 5:05 AM
Lemon Kat
Lemon Kat - avatar
0
I've only done a few lessons from introduction to python. im currently learning pseudocode and a python at school
10th May 2023, 12:18 PM
Lemon Kat
Lemon Kat - avatar
0
Total = 0 Num = float(input()) for Count in range(5): Total = Total + Num print("The total is", Total) """ Input: 3 Output: 15.0 #Explanation: float(3) = 3.0 Total = 0 for Count in range(5): Total = Total+ Num So, 0+3.0 = 3.0 >> range 1 3.0+3.0 = 6.0 >> range 2 6.0+3.0 = 9.0 >> range 3 9.0+3.0 = 12.0 >> range 4 12.0+3.0 = 15.0 >> range 5 """
12th May 2023, 3:30 AM
Fꫀⲅძ᥆͟ᥙ᥉᯽
Fꫀⲅძ᥆͟ᥙ᥉᯽ - avatar
0
nums=input() num=[float(i) for i in nums if i!=" "] print(sum(num)) # try this cod but input all numbers in on line with spaces between them #Ex: input will be like this: 3 7 5 3 2 #output will be: 20.0 #code explination: # first we will think how we can make all numbers in list then use sum() function. #if we input: 3 4 5 #then nums='3 4 5' #in second line we used "List Comprehensions" you can get more information about it in intermediate python lessons in sololearn. # so if our code was like this num=[i for i in nums] print(num) #output will be : [ '3', ' ' , '4' , ' ' , '5' ] #notice numbers saved in list as string and we have spaces not just numbers so if we want to remove the spaces we will use if condition. num=[i for i in nums if i !=" "] print(num) #output will be: [ '3' , '4' , '5'] #notice we removed spaces. now we just have one small problem which is number's type saved as string we can solve that just by using int() or float() as you needed num=[float(i) for i in nums if i != " "] print(num)
12th May 2023, 3:58 AM
Muhammad Ahmad Rasheed
Muhammad Ahmad Rasheed - avatar
0
Muhammad Ahmad Rasheed your approach won't work for multidigit numbers in the input. Try "12 34" as an input. Also it doesn't work correctly if the numbers are on separate lines in the input.
12th May 2023, 8:34 AM
Евгений
Евгений - avatar
0
Muhammad Ahmad Rasheed what about numbers on separate lines?
12th May 2023, 9:10 AM
Евгений
Евгений - avatar
0
Just put it in one line as i mentioned before
12th May 2023, 9:18 AM
Muhammad Ahmad Rasheed
Muhammad Ahmad Rasheed - avatar
0
Muhammad Ahmad Rasheed You mean, just change the task requirements, if one can't solve it? :)
12th May 2023, 9:20 AM
Евгений
Евгений - avatar