Beginner Question - Very Easy I'm Sure...
Todd_Calhoun
anon at anon.com
Fri Mar 25 16:36:31 EST 2005
Thanks for the tip. I knew it was something easy like that.
"Brian van den Broek" <bvande at po-box.mcgill.ca> wrote in message
news:mailman.836.1111700973.1799.python-list at python.org...
> 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
>
More information about the Python-list
mailing list