Steini
Steini

Reputation: 23

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 iiiii*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)

Upvotes: 2

Views: 648

Answers (3)

Piyush S. Wanare
Piyush S. Wanare

Reputation: 4933

This will work for you:-

import random

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

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

Upvotes: 2

Luke
Luke

Reputation: 774

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.

Upvotes: 1

MayTheSchwartzBeWithYou
MayTheSchwartzBeWithYou

Reputation: 1177

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.

Upvotes: 4

Related Questions