JavaScript
x
19
19
1
import random
2
3
4
def add_sums(Num):
5
total = 0
6
all_num = []
7
for x in range(1, Num+1):
8
gen = random.randint(1, 30)
9
all_num.append(gen)
10
11
print("List:", all_num)
12
for y in all_num:
13
total += y
14
print("List total:", total)
15
16
17
user_max = int(input("Max numbers in list: "))
18
add_sums(user_max)
19
In this program, the user will enter the total amount of numbers in a list.
the random
module will generate random numbers between 1
to 30
.
Then all the numbers from the list will be added together.
I’ve tried to use the variable x
but it doesn’t give the results I want, does anyone know a better/simpler way of creating this program. Also, is it bad practice to create a variable and not call it?
Advertisement
Answer
Maybe this better/simpler way of creating this program
JavaScript
1
13
13
1
import random
2
3
def add_sums(Num):
4
5
all_num = []
6
for x in range(Num):
7
all_num.append(random.randint(1, 30))
8
print("List:", all_num)
9
print("List total:", sum(all_num))
10
user_max = int(input("Max numbers in list: "))
11
12
add_sums(user_max)
13