我正在尝试将数组元素读取为
4 #no. of elements to be read in array
1 2 3 4
我通过引用其他答案所尝试的
def main():
n=int(input("how many number you want to enter:"))
l=[]
for i in range(n):
l.append(int(input()))
如果我输入为
4 #no. of elements to be read
1
2
3
4
但如果我尝试给予喜欢
4 #no. of element to be read
1 2 3 4
我得到错误:
ValueError: invalid literal for int() with base 10: '1 2 3 4'
请在这件事上给予我帮助
由于 Python 中没有输入分隔符,因此您应该使用
split
并拆分您从用户那里收到的输入: