Learning question...

Discussion in 'Python' started by swapsun@gmail.com, May 7, 2008.

  1. Guest

    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)
    ################################
     
    , May 7, 2008
    #1
    1. Advertising

  2. wrote:

    > 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
     
    Diez B. Roggisch, May 7, 2008
    #2
    1. Advertising

  3. Thomas Woelz Guest

    On 7 maio, 09:25, wrote:
    > 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
     
    Thomas Woelz, May 7, 2008
    #3
  4. Guest

    On May 7, 8:36 am, "Diez B. Roggisch" <> wrote:
    > wrote:
    > > 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!
     
    , May 7, 2008
    #4
  5. Guest

    On May 7, 8:42 am, Thomas Woelz <> wrote:
    > On 7 maio, 09:25, wrote:
    >
    > > 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


    Thanks very much for the advice. Will follow...
     
    , May 7, 2008
    #5
  6. Terry Reedy Guest

    <> wrote in message
    news:...
    | 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
     
    Terry Reedy, May 7, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James White

    A question about learning/schools.

    James White, Feb 16, 2004, in forum: Perl
    Replies:
    1
    Views:
    463
  2. DaLoverhino

    Learning J2ee, got newbie question.

    DaLoverhino, Feb 21, 2006, in forum: Java
    Replies:
    3
    Views:
    439
    Paul Hamaker
    Feb 22, 2006
  3. Danny
    Replies:
    0
    Views:
    334
    Danny
    Jul 12, 2004
  4. Hal Vaughan
    Replies:
    7
    Views:
    481
  5. Andrey Popp

    [I'm learning C]: Learning to use ucontext

    Andrey Popp, Jan 29, 2012, in forum: C Programming
    Replies:
    5
    Views:
    737
    Keith Thompson
    Jan 31, 2012
Loading...

Share This Page