簡體   English   中英

如何根據用戶輸入構建矩陣?

[英]How to construct a matrix based on user input?

如何解決使用隨機數(用戶輸入)創建矩陣的問題? 我也有這個問題:

UnicodeDecodeError:“ascii”編解碼器無法解碼位置 23 中的字節 0xc3:序號不在范圍內(128)

array = list()
i = int(input("How many row: "))      
j = int(input("How many column: "))    


for x in range(i):                
    if i <= 0 or i>10:
        print("Failure!")  
        break
    elif j <= 0 or j>10:
        print("Failure!")
        break
    else:
        for y in range(j):        
            num = raw_input("Value: ")       
            array.append(int(num))


a = np.reshape((i,j))
print a

我將np.reshape((i,j))更改為a = np.reshape(array,(i,j))並且它工作正常。 我沒有看到您提到的代碼中有任何錯誤。

import numpy as np
array = list()
i = int(input("How many row: "))
j = int(input("How many column: "))


for x in range(i):
    if i <= 0 or i>10:
         print("Failure!")
         break
    elif j <= 0 or j>10:
         print("Failure!")
         break
    else:
        for y in range(j):
             num = raw_input("Value: ")
             array.append(int(num))


a = np.reshape(array,(i,j))
print a

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM