2015-04-06 91 views
0

你好真棒編碼人!Python - 如何添加一個隨機數列表?

有沒有人有一個好主意,添加一個隨機數列表?我想獲得一個列表來記錄循環內部生成的隨機數。這裏是循環中的代碼示例:

stuff = {'name': 'Jack', 'age': 30, 'height': '6 foot 9 inches'} 

tester = [0] 

print(tester) 

tester.append[random.randint(1, len(stuff))] 

print(tester) 

顯然random.randint的輸出不是標化的,但我不知道怎麼回事,寫這個。

非常感謝您的幫助!

+0

我的猜測:你想生成4張隨機數,並把它添加到列表測試? – Ajay 2015-04-06 03:00:25

回答

2
tester.append[random.randint(1, len(stuff))] 
#  wrong^      wrong^

# should be 
tester.append(random.randint(1, len(stuff))) 

方法,如append,用圓括號而不是方括號調用。

0

很簡單,試試這個

from random import randint # import randint from random 
listone = [] # Creating a list called listone 
for i in xrange(1,10): # creating a loop so numbers can add one by one upto 10 times 
    ic = randint(1,10) # generating random numbers from 1 to 10 
    listone.append(ic) # append that numbers to listone 
    pass 
print(listone) # printing list 
# for fun you can sort this out ;) 
print(sorted(listone)) 
相關問題