简体   繁体   中英

how to multiply integers from a random list

I have already imported random and made the list but I have to multiply all the integers from that list and print the outcome. But with this setup, I just get a whole bunch of numbers repeated again and again. I have tried other setups but none of them seem to work for this specific problem. It should be if (i) stands for numbers in list i i i i i*i... and so on and then just print the outcome???

listi=[]

    for i in range(50):
        listi.append(random.randint(5,70))
        teljari=1
        listi=listi
        for x in listi:
            teljari*=x
            print(teljari)

More pythonesque would be

random_numbers = random.sample(range(5, 70), 50)
teljari = 1
other_list = [i * teljari for i in random_numbers]

Thus you get all the random numbers with one command and then you can multiply with whichever process you want.

This will work for you:-

import random

uniqueRandomNumberList = random.sample(range(5,70), 50)

reduce(lambda x, y: x*y,uniqueRandomNumberList)

It kept printing all the numbers, as you had your print function in the for loop, so each time the loop reached the next integer, it printed the product.

from random import randint
listi = []

for x in range(50):
    listi.append(randint(5,70))

teljari = 1

for i in listi:
    teljari *= i

print(teljari)

I think this should work as intended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM