Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Python2.7
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 22-Feb-13 18:02pm
Valmach141
Edited 22-Feb-13 19:10pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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"
  Permalink  
Comments
Valmach at 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 at 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 at 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 at 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)

  Print Answers RSS
0 Marcin Kozub 330
1 OriginalGriff 256
2 Sergey Alexandrovich Kryukov 215
3 Praneet Nadkar 197
4 Richard MacCutchan 182
0 OriginalGriff 8,048
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 23 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100