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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Todd_Calhoun

    #1

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

    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.randrang e(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?


  • Jaime Wyant

    #2
    Re: Beginner Question - Very Easy I'm Sure...

    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 <anon@anon.co m> wrote:[color=blue]
    > 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.randrang e(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
    >[/color]

    Comment

    • Brian van den Broek

      #3
      Re: Beginner Question - Very Easy I'm Sure...

      Todd_Calhoun said unto the world upon 2005-03-24 16:13:[color=blue]
      > 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.randrang e(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?[/color]

      Hi,

      you call str(rannum) but don't store it. Try it like this:
      [color=blue][color=green][color=darkred]
      >>> import random
      >>> word = "dog"
      >>> rannum = random.randrang e(100,999)
      >>> str(rannum)[/color][/color][/color]
      '773'[color=blue][color=green][color=darkred]
      >>> type(rannum)[/color][/color][/color]
      <type 'int'>[color=blue][color=green][color=darkred]
      >>> rannum = str(rannum)
      >>> new_word = word + rannum
      >>> print new_word[/color][/color][/color]
      dog773

      or,
      [color=blue][color=green][color=darkred]
      >>> rannum = str(random.rand range(100,999))
      >>> word + rannum[/color][/color][/color]
      'dog287'[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      HTH,

      Brian vdB

      Comment

      • Todd_Calhoun

        #4
        Re: Beginner Question - Very Easy I'm Sure...

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


        "Brian van den Broek" <bvande@po-box.mcgill.ca> wrote in message
        news:mailman.83 6.1111700973.17 99.python-list@python.org ...[color=blue]
        > Todd_Calhoun said unto the world upon 2005-03-24 16:13:[color=green]
        >> 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.randrang e(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?[/color]
        >
        > Hi,
        >
        > you call str(rannum) but don't store it. Try it like this:
        >[color=green][color=darkred]
        > >>> import random
        > >>> word = "dog"
        > >>> rannum = random.randrang e(100,999)
        > >>> str(rannum)[/color][/color]
        > '773'[color=green][color=darkred]
        > >>> type(rannum)[/color][/color]
        > <type 'int'>[color=green][color=darkred]
        > >>> rannum = str(rannum)
        > >>> new_word = word + rannum
        > >>> print new_word[/color][/color]
        > dog773
        >
        > or,
        >[color=green][color=darkred]
        > >>> rannum = str(random.rand range(100,999))
        > >>> word + rannum[/color][/color]
        > 'dog287'[color=green][color=darkred]
        > >>>[/color][/color]
        >
        > HTH,
        >
        > Brian vdB
        >[/color]


        Comment

        Working...