Learning question...

S

swapsun

Any idea why the following program does not work? I was learning IO on
Python and the following generates a TypeError: range() integer end
argument expected, got str.
I am a beginner.

################################
# print input name (str k), j times using raw_input

def hello():
j=raw_input("Please type in the number of times you want to print
")
k=raw_input("Please type in your name ")
printname(j,k)

def printname(j,k):
for i in range(j):
print ("Hello %s" % k)
################################
 
D

Diez B. Roggisch

Any idea why the following program does not work? I was learning IO on
Python and the following generates a TypeError: range() integer end
argument expected, got str.
I am a beginner.

Because raw_input does return you as string which you need explicitly
convert to a number, e.g. doing

i = int(input)

Diez
 
T

Thomas Woelz

Any idea why the following program does not work? I was learning IO on
Python and the following generates a TypeError: range() integer end
argument expected, got str.
I am a beginner.

################################
# print input name (str k), j times using raw_input

def hello():
j=raw_input("Please type in the number of times you want to print
")
k=raw_input("Please type in your name ")

# add this:
j = int(j)
printname(j,k)

def printname(j,k):
for i in range(j):
print ("Hello %s" % k)
################################

theres too much whitespace in the printname function, try to stick
with 4 spaces (and no tabs) for each block
 
S

swapsun

    # add this:
    j = int(j)



theres too much whitespace in the printname function, try to stick
with 4 spaces (and no tabs) for each block

Thanks very much for the advice. Will follow...
 
T

Terry Reedy

| Any idea why the following program does not work? I was learning IO on
| Python and the following generates a TypeError: range() integer end
| argument expected, got str.
| I am a beginner.
|
| ################################
| # print input name (str k), j times using raw_input
|
| def hello():
| j=raw_input("Please type in the number of times you want to print
| ")
| k=raw_input("Please type in your name ")
| printname(j,k)
|
| def printname(j,k):
| for i in range(j):
| print ("Hello %s" % k)

For future debugging, use print to find out what values you actually have.
print j,k #before printname()
would have shown you that j is a string.
Also use the type() function as needed.
And do read the Lib Ref sections on builting functions and types.

tjr
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top