0
所以,我有一個小問題女巫創建一個Python中的二維數組的隨機數字。Python randomnumber數組
我有一個矩陣看起來像這樣
matrix:
row = ['.' for i in range(w)]
self.matrix = [row]
for j in range(h - 1):
row = ['.' for i in range(w)]
self.matrix.append(row)
w = int(input("please insert the width of the array: "))
h = int(input("please insert the height of the array: "))
user = int(input("please insert a number for objects: "))
x = random.sample(range(w), user)
y = random.sample(range(h), user)
list(zip(x, y))
所以,現在我想建立這麼多的隨機數作爲用戶輸入。例如,用戶輸入100,我需要x和y中的100個數字,以便我可以使用它們作爲位置,將對象隨機插入到數組中。
但是,如果寬度和高度都比較小,那麼userinput會出錯。
什麼是錯誤? –
This example「in raise ValueError(」Sample large than population「)」 – PythonUserNew
您的'user'值大於'len(range(w))'(或'len(range(h))') – JCVanHamme