Click here to Skip to main content
15,918,516 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Write a for loop that sums the values 1 through end, inclusive. end is a variable cannot Use A Variable Called 'sum'

"end" to be 6, your code should print out the result:
21
which is 1 + 2 + 3 + 4 + 5 + 6.

do not include raw_input statements or define the variable end. so write your code in the following box assuming end is already defined.

MY CODE.. "end" variable is already defined, so cannot define it..

total = 0
index = 1
for i in index <= end:
index += 1
print (total)
Posted
Updated 22-Feb-13 18:10pm
v2

1 solution

question one : why python 2.7 ?
question two : please be clear on what you need to achieve with your code

"in order to get right answer u need to ask right question"
 
Share this answer
 
Comments
Valmach 23-Feb-13 0:08am    
does not necessarily have to be 2.7. but that's what i am learning on. As a beginner student I am trying figure out why if the variable "end" has been "predefined, and cannot use the variable sum or enter "raw_data" as a matter of fact no built-in functions are allowed.. the code should get = 21 without using "built-in" functions - so.. i tried the code above and when the Teacher checks.. Wrong answer. .I hope this explains the situation better
Creepystranger 25-Feb-13 0:49am    
although still not very clear to me but if i am correct ur trying to suggest that u need to write a code which will add upto the "end" where end is predefined and u can not use any variable like sum for adding numbers

what i understand from ur question that u do not want to use any other variable like total so

i would suggest u to forget the for loop and use math's simple algorithm

1+2+3....+n= n(n+1)/2 .
Valmach 25-Feb-13 1:32am    
As described to me..
Write a for loop that sums the values 1 through end, inclusive. end is a variable. the defined end is to be 6, code should print out the result:

21
which is 1 + 2 + 3 + 4 + 5 + 6.

do not include raw_input statements or define the variable end. - so write code assuming end is already defined.

Don't Use A Variable Called 'sum'
Valmach 25-Feb-13 1:32am    
also must use a For Loop

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900