What should I do if I have a variable number of variables and I want the sum of variables? For Eg.
import random
X = random.randint(1, 25)
Y = random.randint(1, 5) X
for A in range(X, Y):
W = random.randint(1, 10) # you want sum of all W produced
# W = W W --> will not work
From this very random system, I want to extract the sum of all W.
something like W = W W won't work, the value of the previous result cannot be stored
CodePudding user response:
import random
X = random.randint(1, 25)
Y = random.randint(1, 5) X
W = 0
for A in range(X, Y):
W = random.randint(1, 10)
print(W)
Based on @Sembei Norimaki answer
CodePudding user response:
You don't even need a for loop for that, simply sum over a generator expression.
import random
X = random.randint(1, 25)
Y = random.randint(1, 5) X
W = sum(random.randint(1, 10) for _ in range(X, Y))