Home > OS >  python - random number and calculations dont work together
python - random number and calculations dont work together

Time:10-21

I try to generate a random number, then substract this number as percentage from my original price with following code:

rndnm = random.sample(range(10, 40), 1)
priceraw = int(price) - (int(price)/100*int(rndnm))
saleprice = str(priceraw)
print(saleprice)

nothing gets printed with this. how should i write it that it works?

CodePudding user response:

You generate a list of random numbers. You need to get the first one:

rndnm = random.sample(range(10, 40), 1)[0]

CodePudding user response:

Are you trying to do this?

import random

price = 5000

rndnm = random.randint(10, 40)
print(rndnm)

priceraw = int(price) - (int(price)/100*int(rndnm))
saleprice = str(priceraw)
print(saleprice)

#Output
28
3600.0


  • Related