For my IT class we need to do 10 different tasks and this is the last one that I'm completely stumped on. The goal is to make a list with 20 numbers between -500 and 500. We have two lines of code to sample off of and finish this with.
对于我的IT课程,我们需要完成10个不同的任务,这是我完全难以理解的最后一个任务。我们的目标是制作一个包含介于-500和500之间的20个数字的列表。我们有两行代码可用于抽样并完成此操作。
for i in range(10)
print(i)
and
import random
print (random.randint(0,100))
Please help me figure this out so I can get a better understanding of the code.
请帮我解决这个问题,这样我就能更好地理解代码。
2 个解决方案
#1
1
Building off of @COLDSPEED's comment, if you want a list of 20 number different random numbers between -500 and 500. You can use a while loop to build a set of the random numbers, then convert it to a list.
建立@COLDSPEED注释,如果你想要一个在-500和500之间的20个数字不同随机数的列表。你可以使用while循环来构建一组随机数,然后将其转换为列表。
import random
rand_set = set()
while len(rand_set) < 20:
x = random.randint(-500,500)
if x not in rand_set:
rand_set.add(x)
rand_list = list(rand_set)
#2
0
use this
import random
lst=[]
for i in range(20):
lst.append (random.randint(-500, 500))
print (lst)
#1
1
Building off of @COLDSPEED's comment, if you want a list of 20 number different random numbers between -500 and 500. You can use a while loop to build a set of the random numbers, then convert it to a list.
建立@COLDSPEED注释,如果你想要一个在-500和500之间的20个数字不同随机数的列表。你可以使用while循环来构建一组随机数,然后将其转换为列表。
import random
rand_set = set()
while len(rand_set) < 20:
x = random.randint(-500,500)
if x not in rand_set:
rand_set.add(x)
rand_list = list(rand_set)
#2
0
use this
import random
lst=[]
for i in range(20):
lst.append (random.randint(-500, 500))
print (lst)