StackOverflow用户晚上好!我正在开发一个python代码,它可以执行以下操作:
用户将输入两个3x3矩阵的值,然后从包括加法、减法、矩阵乘法和逐元素乘法在内的选项中进行选择。应该使用
numpy.matmul()
进行矩阵乘法运算(例如np.matmul(a, b)
(。程序应计算适当的结果并返回结果、结果的转置、结果的行平均值和结果的列平均值
然而,我所拥有的代码会产生如下错误消息。
Traceback (most recent call last):
File "C:/Users/jwhoc/OneDrive/Documents/Python Tests/week4_lab.py", line 40, in <module>
print(a[i][j],end=" ")
IndexError: list index out of range
此错误消息发生在您第一次输入3x3矩阵之后。我将在下面插入代码。
import numpy as np
import re
print("*******************************Welcome to the Python Matrix Application*******************")
while True:
print("Do you want to play the Matrix Game?")
#Reading the choice
choice=input("Enter Y for yes or N for No:")
if choice=="N":
print("****************Thanks for playing Python Numpy************************")
break
else:
while True:
phone=input("Enter your phone number(XXX-XXX-XXXX):")
#Regular expression for checking the phone number format
if not re.match("d{3}-d{3}-d{4}",phone):
print("Your phone number is not in correct format. Please reenter:")
else:
break
while True:
zip=input("Enter your zipcode+4(XXXXX-XXXX):")
#Regular expression for checking the zipcode format
if not re.match("d{5}-d{4}",zip):
print("Your zipcode is not in correct format. Please reenter:")
else:
break
#Reading the first matrix
print("Enter your first 3x3 matrix:")
a=[]
for i in range(3):
#Reading row by row
row=input().split()
#Converting each element to integer
row=list(map(int,row))
#Adding row to the matrix
a.append(row)
#Printing first matrix
print("Your first 3x3 matrix is:")
for i in range(3):
for j in range(3):
print(a[i][j],end=" ")
print()
你们能提供的任何帮助都将是非常棒的。谢谢大家!
我运行了您的代码,它运行得很好,但我认为您可能遇到的问题是引入输入的形式。例如:如果你以这种方式提供行,就没有问题:
2 5 6
但如果你这样做:
256
你会得到与你说的相同的错误,这是因为当你呼叫时
row = input().split()
您得到的不是[2, 5, 6]
,而是[256]
。这是因为split((函数的默认分隔符是一个空格。如果你想以这种方式引入值256
,你应该将row = input().split()
更改为
row = input().split("")
请注意">
问题是矩阵输入不符合所需的格式。CCD_ 7在"字符,除非另有规定,否则数字必须用空格分隔。我认为对矩阵输入进行一些类似于早期输入的错误检查可能会有所帮助。试试这个:
import re
print("*******************************Welcome to the Python Matrix Application*******************")
while True:
print("Do you want to play the Matrix Game?")
#Reading the choice
choice=input("Enter Y for yes or N for No:")
if choice=="N":
print("****************Thanks for playing Python Numpy************************")
break
else:
while True:
phone=input("Enter your phone number(XXX-XXX-XXXX):")
#Regular expression for checking the phone number format
if not re.match("d{3}-d{3}-d{4}",phone):
print("Your phone number is not in correct format. Please reenter:")
else:
break
while True:
zip=input("Enter your zipcode+4(XXXXX-XXXX):")
#Regular expression for checking the zipcode format
if not re.match("d{5}-d{4}",zip):
print("Your zipcode is not in correct format. Please reenter:")
else:
break
#Reading the first matrix
print("Enter your first 3x3 matrix:")
a=[]
for i in range(3):
while True
#Reading row by row
row=input().split()
#Converting each element to integer
row=list(map(int,row))
if len(row) != 3:
print(f"Needs 3 numbers. {len(row)} numbers specified. Please re-enter:")
else:
break
#Adding row to the matrix
a.append(row)
#Printing first matrix
print("Your first 3x3 matrix is:")
for i in range(3):
for j in range(3):
print(a[i][j],end=" ")
print()