想在 python 3+ 中只使用一个用户定义的输入和输出函数
want to use only one user define function for input and output in python 3+
from numpy import*
def row():
for i in range(len(a)):
for j in range(len(a[i])):
t = [i, j]
inp(*t)
def inp(*m):
a[m] = int(input(f"entert the element of {m} = "))
out(*m)
def out(*o):
print(a[o])
a = zeros((1,2), dtype = int)
row()
输出显示如下:
enter the element of (0, 0) = 2
2
enter the element of (0, 1) = 3
3
但我想像这样显示输出
像这样一次输入
enter the element of (0, 0) = 2
enter the element of (0, 1) = 3
这样一次输出
2
3
可以通过创建两个单独的 for 循环或两个用于输入和输出的函数来实现
但我的问题是:我怎样才能只创建一个函数
def row():
for i in range(len(a)):
for j in range(len(a[i])):
t = [i, j]
inp(*t)
与其立即打印输出,不如保存值并在完成收集后打印它们:
import numpy as np
def row(a):
values = []
for i in range(len(a)):
for j in range(len(a[i])):
value = get_input(a, i, j)
values.append(value)
for value in values:
print(value)
def get_input(a, *m):
a[m] = int(input(f"enter the element of {m} = "))
return a[m]
a = np.zeros((1,2), dtype = int)
row(a)
给予
enter the element of (0, 0) = 1
enter the element of (0, 1) = 2
1
2
但是,还有一种更简单的方法:因为您还在矩阵中设置了新元素,所以您不必单独保存它们,只需打印出矩阵值即可:
for item in a.ravel():
print(item)
from numpy import*
def row():
for i in range(len(a)):
for j in range(len(a[i])):
t = [i, j]
inp(*t)
def inp(*m):
a[m] = int(input(f"entert the element of {m} = "))
out(*m)
def out(*o):
print(a[o])
a = zeros((1,2), dtype = int)
row()
输出显示如下:
enter the element of (0, 0) = 2
2
enter the element of (0, 1) = 3
3
但我想像这样显示输出
像这样一次输入
enter the element of (0, 0) = 2
enter the element of (0, 1) = 3
这样一次输出
2
3
可以通过创建两个单独的 for 循环或两个用于输入和输出的函数来实现 但我的问题是:我怎样才能只创建一个函数
def row():
for i in range(len(a)):
for j in range(len(a[i])):
t = [i, j]
inp(*t)
与其立即打印输出,不如保存值并在完成收集后打印它们:
import numpy as np
def row(a):
values = []
for i in range(len(a)):
for j in range(len(a[i])):
value = get_input(a, i, j)
values.append(value)
for value in values:
print(value)
def get_input(a, *m):
a[m] = int(input(f"enter the element of {m} = "))
return a[m]
a = np.zeros((1,2), dtype = int)
row(a)
给予
enter the element of (0, 0) = 1
enter the element of (0, 1) = 2
1
2
但是,还有一种更简单的方法:因为您还在矩阵中设置了新元素,所以您不必单独保存它们,只需打印出矩阵值即可:
for item in a.ravel():
print(item)