M. Pollino
M. Pollino

Reputation: 45

Turn 2 integers in to 1

I have a method that is retrieving a couple of random numbers. I then want to combine randoma and randomb to be one number

For example if randoma = 2 and randomb = 150, I want it to return 2150

I am unsure how to do so. The #'s are where my unknown return statement would be.

def display()
    total = setnums()
    print("total = " + total)

def setnums():
    randoma = random.randint(1, 5)
    randomb = random.randint(100,1000)
    return ########

Upvotes: 2

Views: 333

Answers (6)

maxrodrigo
maxrodrigo

Reputation: 1850

TL;DR:

In Python >= 3.6 use f-strings:

return int(f'{randoma}{randomb}')

In Python < 3.6 use the + Operator:

int(str(randoma) + str(randomb))

Keep it simple, readable, and test in your own environment which option suits you best.


Given the following function:

def setnums():
    randoma = random.randint(1, 5)
    randomb = random.randint(100, 1000)
    r = ##
    return int(r)

Execution time in python3.6:

r = f"{randoma}{randomb}"
2.870281131
r = "%s%s" % (randoma, randomb)
2.9696586189999996
r = str(randoma) + str(randomb)
3.084615994999999
r = "".join((str(randoma), str(randomb)))
3.1661511100000013
def setnums():
    randoma = str(random.randint(1, 5))
    randomb = str(random.randint(100, 1000))
    randoma += randomb
    return int(randoma)

3.0611202350000006

Execution time in python2.7:

r = "%s%s" % (randoma, randomb)
2.46315312386
r = str(randoma) + str(randomb)
2.56769394875
r = "".join((str(randoma), str(randomb)))
2.68126797676
def setnums():
    randoma = str(random.randint(1, 5))
    randomb = str(random.randint(100, 1000))
    randoma += randomb
    return int(randoma)

2.53426408768

Literal String Interpolation (PEP 498).
Splitting, Concatenating, and Joining Strings in Python

Upvotes: 0

Nathanael Demacon
Nathanael Demacon

Reputation: 1217

In Python 3.6 and newer:

int(f'{randoma}{randomb}')

In Python older than 3.6:

int(str(randoma) + str(randomb))

Upvotes: 6

oziomajnr
oziomajnr

Reputation: 1751

If you want to do it using simple mathematic without converting to string

from math import floor, log10, pow
from random import randint

def display():
    total = setnums()
    print(total)

def setnums():
     randoma = randint(1, 5)
     randomb = randint(100,1000)
     print(randoma)
     print(randomb)
     numberOfDigitsInFirstRandNumber = floor(log10(randomb))
     numberOfDigitsInSecondRandNumber = floor(log10(randomb))
     totalDigitCount =  numberOfDigitsInFirstRandNumber + numberOfDigitsInSecondRandNumber
     multiplyingFactor = pow(10, totalDigitCount - 1)
     return (randoma * multiplyingFactor ) + randomb

display()

Basically what you do is to sum the number of digits in both numbers, subtract one from it and raise 10 to the power of the result, then multiple the first random number by the result and add it to the second random number.

you can try it out from here https://repl.it/repls/StainedRashArchive

Upvotes: 0

Jurgen Cuschieri
Jurgen Cuschieri

Reputation: 736

Use the 'str' function to convert randoma and randomb to strings, concatenate by using '=', then convert the concatenated string to integer by using the 'int' function.

Upvotes: -2

Bill the Lizard
Bill the Lizard

Reputation: 405965

Convert them to strings and concatenate them.

return int(str(randoma) + str(randomb))

Upvotes: 2

Govert
Govert

Reputation: 16907

int(str(randoma) + str(randomb))

Upvotes: 0

Related Questions