[Tutor] raw_input into range() function

Ben Sherman bensherman at gmail.com
Wed Apr 18 20:25:02 CEST 2007


On 4/18/07, Guba <listsdl04 at yahoo.de> wrote:
> Hello,
>
> I am trying to do the exercises in Michael Dawson's "Absolute Beginner"
> book. In chapter four ("for Loops, Strings, and Tuples") one of the
> challenges is: "Write a program that counts for the user. Let the user
> enter the starting number, the ending number, and the amount by which to
> count."
>
> The code I have come up with so far is further below; basically my
> problem is that I don't know how to feed the range() function with the
> user-input numbers it expects.
>
> Your help is highly appreciated!
>
> Guba
>
>
> # Counting Program
> # 2007-04-18
>
> # Welcoming the player
> print "Hello, let me do some counting for you!"
>
> # Telling the player what to do & assigning that info to variables.
> start_num = int(raw_input("Please give me a starting number. "))
> end_num = int(raw_input("Please give me an ending number. "))
> interval = int(raw_input("By which amount am I to count? "))
>
> start_num == 0
> end_num == 1
> interval == 2
>
> print "Counting:"
> for i in range(0, 1, 2):
>      print i
>
>
> raw_input("\n\nHit Enter to exit.")


Your attempt to read input is never used, and your variable
assignments are not correct.

You are using the test operator '==' instead of the assignment
operator '='.  The lines:
"""
start_num == 0
end_num == 1
interval == 2
"""
do something you aren't trying to do here.  Those lines are testing to
see if start_num equals zero, and then ignoring what the test result
is.  They don't actually *do* anything.

Your code should look like this:

print "Hello, let me do some counting for you!"

start_num = int(raw_input("Please give me a starting number. "))
end_num = int(raw_input("Please give me an ending number. "))
interval = int(raw_input("By which amount am I to count? "))

print "Counting:"
for i in range(start_num, end_num, interval):
    print i


More information about the Tutor mailing list