I want to use a while loop to count up numbers.
The output would be something like this:
Start: 1
End: 10
Count by: 3
1 4 7 10

Here's my code right now:

start = int(raw_input("Start at: "))
end = int(raw_input("End at: "))
count = int(raw_input("Count by: "))

while :
        print i,

I'm not sure what to put in for the while loop.

------------------------------------------------------------------
For another test, I want to try to reverse the strings printed.

The output would be this:
Input a string: Hello!
Reverse: !olleH

I have 2 different codes for this.

First code:

s = raw_input("Enter a string: ")
rev = ""
for i in range(len(s)):
#i only want to modify the "s[ ]" part, don't want to change any other part of the code
    rev = rev + s[i-1]
print "The reverse is: " + rev

Second code:

s = raw_input("Enter a string: ")
rev = ""
#for this code, I only want to modify the "range( )" part of the code, i don't want to change any other part of the code
for i in range():
    rev = rev + s[i]
print "The reverse is: " + rev

any help is appreciated, if you can, please don't reply with any really complicated codes.

explainations would be helpful too so i can learn more.

thanks!

Recommended Answers

All 8 Replies

for your first question

start = int(raw_input("Start at: "))
end = int(raw_input("End at: "))
count = int(raw_input("Count by: "))
 
while start <= end:
        print start
        start = start + count

and for your second question have a look at this and try and figure it out

x ='hello!'
rev = ''
for i in range(1,len(x)+1):
    rev += x[-1*i]
    
print rev
Member Avatar for masterofpuppets

hi
for the first problem try:

start = int(raw_input("Start at: "))
end = int(raw_input("End at: "))
count = int(raw_input("Count by: "))
 
while start <= end:
    print start
    start += count

basically all you have to do is have a loop that loops until the value of start is less than the value of the end. every time print the value of start before it'u updated and after that update it with the value of the counter :)

as for the second part I used this:

s = raw_input("Enter a string: ")
rev = ""
for i in range( 1, len( s ) + 1 ):
    rev = rev + s[ -i ]
print "The reverse is: " + rev

if the string is "hello!" the condition of the loop is 1, len( "hello!" ) so its ( 1, 6 ) and s[ -i ] = -1( because we start the loop from 1 ), -2, -3, -4, -5, -6( need to add 1 to the length of the string because the range function starts from the first parameter but ends before the second parameter )
hope that helps. if you don't understand something i'll try to explain in more detail :)

My 2 cents...
1 - I prefer for loop than while ones when I can (less code to write and no infinite loops).

start = int(raw_input("Start at: "))
end = int(raw_input("End at: "))
count = int(raw_input("Count by: "))

for i in range(start,end+1,count) : # end + 1 because range considers the end term excluded.
        print i,

2- First code
I put prints for you to understand what happens

s = raw_input("Enter a string: ")
print "len(s)=", len(s)
rev = ""
for i in range(len(s)):
    print "i=", i, "-i-1=", -i-1
#i only want to modify the "s[ ]" part, don't want to change any other part of the code
    rev = rev + s[-i-1] # counting negative way takes the string from the end (s[-1] = last character)
print "The reverse is: " + rev

second code

s = raw_input("Enter a string: ")
rev = ""
#for this code, I only want to modify the "range( )" part of the code, i don't want to change any other part of the code
for i in range(len(s)-1, -1, -1): # range(start, end, count) start included, end excluded, count negative. this will go backward from the end of the string
    rev = rev + s[i]
print "The reverse is: " + rev

Thank you very much.

The explanations helped a lot too ^_^, thanks for the efforts in explaining the codes.

@ jice:
I get how start is included, because starting at -1 is starting at the end, and I get how it counts backwards because of the -1, but what do you mean by the "end is excluded"? BTW your answer for the second code is very helpful, I was about to ask the same question. Thanks!

One short way for second code.

>>> s = raw_input("Enter a string: ")
Enter a string: car
>>> print 'The reverse is %s' % (s[::-1])
The reverse is rac

When I say end is excluded, i mean that :

for i in range(0, 10):
    print i,

> 0 1 2 3 4 5 6 7 8 9

No 10 in the list.
range(0,10) = [ 0, 10 [ in the mathematical writing way
This means that if you want to loop backward from end to start, you have to do

for i in range(len(s)-1, -1, -1):

When I say end is excluded, i mean that :

for i in range(0, 10):
    print i,

> 0 1 2 3 4 5 6 7 8 9

No 10 in the list.
range(0,10) = [ 0, 10 [ in the mathematical writing way
This means that if you want to loop backward from end to start, you have to do

for i in range(len(s)-1, -1, -1):

Oh right! thanks for your help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.