1

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.

3 Answers3

2

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)]])
chepner
  • 497,756
  • 71
  • 530
  • 681
0

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))
a121
  • 798
  • 4
  • 9
  • 20
  • Repeated concatenation with `+` is probably the worst way to join multiple values into a single string. – chepner Feb 19 '21 at 15:16
  • @TheMineSlash this method can also be used: https://stackoverflow.com/a/42149618/14719340 – a121 Feb 19 '21 at 15:25
0

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))
Rohr
  • 17
  • 1
  • 6