Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old June 27th, 2008, 05:23 PM
swapsun@gmail.com
Guest
 
Posts: n/a
Default Learning question...

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)
################################
  #2  
Old June 27th, 2008, 05:23 PM
Diez B. Roggisch
Guest
 
Posts: n/a
Default Re: Learning question...

swapsun@gmail.com wrote:
Quote:
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
  #3  
Old June 27th, 2008, 05:23 PM
Thomas Woelz
Guest
 
Posts: n/a
Default Re: Learning question...

On 7 maio, 09:25, swap...@gmail.com wrote:
Quote:
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)
Quote:
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
  #4  
Old June 27th, 2008, 05:23 PM
swapsun@gmail.com
Guest
 
Posts: n/a
Default Re: Learning question...

On May 7, 8:36*am, "Diez B. Roggisch" <de...@nospam.web.dewrote:
Quote:
swap...@gmail.com wrote:
Quote:
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
Thank you, Diez!
  #5  
Old June 27th, 2008, 05:23 PM
swapsun@gmail.com
Guest
 
Posts: n/a
Default Re: Learning question...

On May 7, 8:42*am, Thomas Woelz <two...@gmail.comwrote:
Quote:
On 7 maio, 09:25, swap...@gmail.com wrote:
>
Quote:
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.
>
Quote:
################################
# print input name (str k), j times using raw_input
>
Quote:
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)
>
Quote:
* * printname(j,k)
>
Quote:
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
Thanks very much for the advice. Will follow...
  #6  
Old June 27th, 2008, 05:23 PM
Terry Reedy
Guest
 
Posts: n/a
Default Re: Learning question...


<swapsun@gmail.comwrote in message
news:d61b9c56-6f9a-4b29-9247-4fce0b80ecbe@f63g2000hsf.googlegroups.com...
| 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





 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles