By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,767 Members | 2,114 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,767 IT Pros & Developers. It's quick & easy.

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

P: n/a
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?
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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 <an**@anon.com> 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

Jul 18 '05 #2

P: n/a
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

Jul 18 '05 #3

P: n/a
Thanks for the tip. I knew it was something easy like that.
"Brian van den Broek" <bv****@po-box.mcgill.ca> wrote in message
news:ma*************************************@pytho n.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

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.