+ 3

How do I get total sum of range?

Number = input("Enter any number you like between 0-10") Sum = 0 for i in range(11): total = number + i Print(total) Please help! Thanks!

16th Jul 2017, 6:38 PM
Keith Crisologo
Keith Crisologo - avatar
9 Antworten
+ 8
Actually: print(sum(range(int(input("Enter no."))))) # that will do the trick, too
16th Jul 2017, 7:20 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 1
Strange... I don't have any syntax error. Here is the code: https://code.sololearn.com/cWrj2CuqFfba/?ref=app
16th Jul 2017, 6:59 PM
Debakar Roy
Debakar Roy - avatar
+ 1
That's just to print a new line. Glad to know that you understand. Keep coding :)
16th Jul 2017, 7:04 PM
Debakar Roy
Debakar Roy - avatar
0
Your logic is not correct. It will be something like this: Number = int(input("Enter any number you like between 0-10")) Total = 0 for i in range(Number+1): Total = Total + i print() print(Total)
16th Jul 2017, 6:45 PM
Debakar Roy
Debakar Roy - avatar
0
First you need to convert the input into integer. Then run the loop from 0 upto the Number. Here 'for i in range(x)' means value of i will be 0, 1, 2,...., (x-1). Moreover you don't need sum when you are doing your calculations using 'Total'. Here initial value of Total will be 0. And inside loop you are adding up the value until a certain range(Number between 0 and 10) given by the user. Also you need to intend the statement after ':' to make the compiler understand that it is inside for loop. It is a basic syntax of python.
16th Jul 2017, 6:50 PM
Debakar Roy
Debakar Roy - avatar
0
Thanks! There is a invalid syntax on Total = 0
16th Jul 2017, 6:55 PM
Keith Crisologo
Keith Crisologo - avatar
0
I appreciate the help! I didn't add the print()
16th Jul 2017, 7:02 PM
Keith Crisologo
Keith Crisologo - avatar
0
Simple basic Maths n = int(input('Enter any number')) print((n*(n+1))/2)
17th Jul 2017, 2:25 AM
Shubh Saxena
Shubh Saxena - avatar
0
i have noticed that the p on print is capital so maybe that is another part of the problem
27th Jan 2022, 1:59 PM
michael
michael - avatar