Beginner Question - Very Easy I'm Sure...

Discussion in 'Python' started by Todd_Calhoun, Mar 24, 2005.

  1. Todd_Calhoun

    Todd_Calhoun Guest

    I'm trying to generate a random number, and then concetate it to a word to
    create a password.

    I get the number and assign it to a variable:

    +++++++++++++++++++++++++++++
    word = "dog"

    import random
    rannum = random.randrange(100,999)

    str(rannum)

    word + rannum
    +++++++++++++++++++++++++++++

    But when I try to concetate the two, I get an error saying:

    ++++++++++++++++++++++++++++
    Traceback (most recent call last):
    File "<pyshell#0>", line 1, in -toplevel-
    list[1] + rannum
    TypeError: unsubscriptable object
    ++++++++++++++++++++++++++++

    Any suggestions?
    Todd_Calhoun, Mar 24, 2005
    #1
    1. Advertising

  2. Todd_Calhoun

    Jaime Wyant Guest

    str() returns a string, it doesn't change rannum which is still a number...

    try ->
    rannum = str(rannum)

    jw

    On Thu, 24 Mar 2005 13:13:25 -0800, Todd_Calhoun <> wrote:
    > I'm trying to generate a random number, and then concetate it to a word to
    > create a password.
    >
    > I get the number and assign it to a variable:
    >
    > +++++++++++++++++++++++++++++
    > word = "dog"
    >
    > import random
    > rannum = random.randrange(100,999)
    >
    > str(rannum)
    >
    > word + rannum
    > +++++++++++++++++++++++++++++
    >
    > But when I try to concetate the two, I get an error saying:
    >
    > ++++++++++++++++++++++++++++
    > Traceback (most recent call last):
    > File "<pyshell#0>", line 1, in -toplevel-
    > list[1] + rannum
    > TypeError: unsubscriptable object
    > ++++++++++++++++++++++++++++
    >
    > Any suggestions?
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    Jaime Wyant, Mar 24, 2005
    #2
    1. Advertising

  3. Todd_Calhoun said unto the world upon 2005-03-24 16:13:
    > I'm trying to generate a random number, and then concetate it to a word to
    > create a password.
    >
    > I get the number and assign it to a variable:
    >
    > +++++++++++++++++++++++++++++
    > word = "dog"
    >
    > import random
    > rannum = random.randrange(100,999)
    >
    > str(rannum)
    >
    > word + rannum
    > +++++++++++++++++++++++++++++
    >
    > But when I try to concetate the two, I get an error saying:
    >
    > ++++++++++++++++++++++++++++
    > Traceback (most recent call last):
    > File "<pyshell#0>", line 1, in -toplevel-
    > list[1] + rannum
    > TypeError: unsubscriptable object
    > ++++++++++++++++++++++++++++
    >
    > Any suggestions?


    Hi,

    you call str(rannum) but don't store it. Try it like this:

    >>> import random
    >>> word = "dog"
    >>> rannum = random.randrange(100,999)
    >>> str(rannum)

    '773'
    >>> type(rannum)

    <type 'int'>
    >>> rannum = str(rannum)
    >>> new_word = word + rannum
    >>> print new_word

    dog773

    or,

    >>> rannum = str(random.randrange(100,999))
    >>> word + rannum

    'dog287'
    >>>


    HTH,

    Brian vdB
    Brian van den Broek, Mar 24, 2005
    #3
  4. Todd_Calhoun

    Todd_Calhoun Guest

    Thanks for the tip. I knew it was something easy like that.


    "Brian van den Broek" <> wrote in message
    news:...
    > Todd_Calhoun said unto the world upon 2005-03-24 16:13:
    >> I'm trying to generate a random number, and then concetate it to a word
    >> to create a password.
    >>
    >> I get the number and assign it to a variable:
    >>
    >> +++++++++++++++++++++++++++++
    >> word = "dog"
    >>
    >> import random
    >> rannum = random.randrange(100,999)
    >>
    >> str(rannum)
    >>
    >> word + rannum
    >> +++++++++++++++++++++++++++++
    >>
    >> But when I try to concetate the two, I get an error saying:
    >>
    >> ++++++++++++++++++++++++++++
    >> Traceback (most recent call last):
    >> File "<pyshell#0>", line 1, in -toplevel-
    >> list[1] + rannum
    >> TypeError: unsubscriptable object
    >> ++++++++++++++++++++++++++++
    >>
    >> Any suggestions?

    >
    > Hi,
    >
    > you call str(rannum) but don't store it. Try it like this:
    >
    > >>> import random
    > >>> word = "dog"
    > >>> rannum = random.randrange(100,999)
    > >>> str(rannum)

    > '773'
    > >>> type(rannum)

    > <type 'int'>
    > >>> rannum = str(rannum)
    > >>> new_word = word + rannum
    > >>> print new_word

    > dog773
    >
    > or,
    >
    > >>> rannum = str(random.randrange(100,999))
    > >>> word + rannum

    > 'dog287'
    > >>>

    >
    > HTH,
    >
    > Brian vdB
    >
    Todd_Calhoun, Mar 25, 2005
    #4
    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. John Hoge

    Very, very, easy question

    John Hoge, May 27, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    375
  2. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    454
    Raymond Hettinger
    Jul 27, 2003
  3. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,594
    Merrill & Michele
    Oct 19, 2004
  4. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    503
    Dave Thompson
    Feb 14, 2005
  5. Replies:
    0
    Views:
    543
Loading...

Share This Page