Re: 廢文

作者: zoeredbird (柔依‧紅鳥)   2023-12-09 11:34:31
import numpy as np
# 選項設置
def matrix_calculator():
print("Matrix Calculator")
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Transposition")
print("5. Exit")
choice = input("Enter your choice (1-5): ")
if choice == '5':
print("Exiting the calculator.")
return
# 矩陣a
if choice in ['1', '2', '3', '4']:
rows_a = int(input("Enter the number of rows: "))
cols_a = int(input("Enter the number of columns: "))
matrix_a = np.empty((rows_a, cols_a))
print("Enter values for Matrix A:")
for i in range(rows_a):
for j in range(cols_a):
value = float(input(f"Enter the value for element A({i+1}, {j+1}
): "))
matrix_a[i, j] = value
# 矩陣b
if choice in ['1', '2', '3']:
rows_b = int(input("Enter the number of rows: "))
cols_b = int(input("Enter the number of columns: "))
matrix_b = np.empty((rows_b, cols_b))
print("Enter values for Matrix B:")
for i in range(rows_b):
for j in range(cols_b):
value = float(input(f"Enter the value for element B({i+1}, {
j+1}): "))
matrix_b[i, j] = value
if choice == '1':
result = matrix_a + matrix_b
operation = "Addition"
elif choice == '2':
result = matrix_a - matrix_b
operation = "Subtraction"
elif choice == '3':
result = np.dot(matrix_a, matrix_b)
operation = "Multiplication"
elif choice == '4':
result = matrix_a.T
operation = "Transposition"
print(f"\nResult of {operation}:\n{result}\n")
matrix_calculator()
else:
print("Invalid choice. Please enter a number from 1 to 5.")
matrix_calculator()
# 驗證矩陣能否運算
matrix_calculator()
# gpt :你確定輸入5要沒有返回任何值,除非遞迴是你要的不然可以考慮使用循環,否則
會產生不必要的遞迴調用
原本想用gpt生成就好一看發現一些問題改了一下變成了億點問題
億點縮排錯誤

Links booklink

Contact Us: admin [ a t ] ucptt.com