Reputation:
I just started learning Python and I want to convert one of my codes to this language. I have a little problem what I can't solve. My script have to make random numbers and printing them after another without adding them together.
time = random.randint(1000000000,9999999999)
no_hash = time+random.randint(1,100)+random.randint(1,10000)+random.randint(1,10000000)
Example output 4245200583 but I need just like this: 423694030332415251234. As you see the code adding the numbers together (because of + between them) but I don't want to add the numbers together just print them.
Upvotes: 1
Views: 1274
Reputation: 531055
Each of your values is an int
, so +
will implement integer addition. You need to convert each int
to a str
first.
You can do that implicitly with various string-formatting operations, such as
time = random.randint(1000000000,9999999999)
no_hash = f'{time}{random.randint(1,100)}{random.randint(1,10000)}{random.randint(1,10000000)}'
or explicitly to be joined using ''.join
:
time = random.randint(1000000000,9999999999)
no_hash = ''.join([str(x) for x in [time, random.randint(1, 100), random.randint(1,10000), random.randint(1,1000000)]])
Upvotes: 2
Reputation: 17
You are adding a type of integer, when you need to add a type of string.
use the str() declaration to make sure Python sees it as string.
For example:
time = random.randint(1000000000,9999999999)
no_hash = str(time)+str(random.randint(1,100))+str(random.randint(1,10000))+str(random.randint(1,10000000))
Upvotes: 0
Reputation: 786
use str()
to concatenate the numbers
time = random.randint(1000000000,9999999999)
no_hash = str(time)+str(random.randint(1,100))+str(random.randint(1,10000))+str(random.randint(1,10000000))
Upvotes: 0