Putting #'s and variables together in 1 variable
Steve Holden
steve at holdenweb.com
Thu Aug 23 17:29:02 EDT 2007
Lamonte Harris wrote:
> File "Desktop\python\newsystem\init.py", line 51, in random_n
> random_name = a+b+c+d+e+ 'temp.txt'
> TypeError: unsupported operand type(s) for +: 'int' and 'str'
>
> import random
> def random_name():
> a = random.randint(0,9)
> b = random.randint(0,9)
> c = random.randint(0,9)
> d = random.randint(0,9)
> e = random.randint(0,9)
> random_name = a+b+c+d+e+ 'temp.txt '
> return random_name
> print random_name
>
> How can i make it so that it adds the random numbers and the temp.txt to
> random_name variable w/out causing any error?
>
random_name = "%d%d%d%d%dtemp.txt" % (a, b, c, d, e)
or
random_name = str(a)+str(b)+str(c)+str(d)+str(e)+"temp.txt"
Unlike, for example, Perl, Python does not attempt many implicit
conversions and instead forces you to say exactly what you mean. This
has the advantage of clarity.
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------
More information about the Python-list
mailing list