我试图在循环中的一行中读取整数。例如,如果我需要读取 5 个 nos,我需要从运行时读取它们作为假设1 3 2 1 2
。问题是我事先不知道需要读取多少个整数,因为它也会在运行时提供。
到目前为止,我已经尝试过这个:
c1=input()
c1=int(c1)
for i in range(c1):
ar[i]=int(input())
但它读取整数为:
1
3
2
1
2
任何人都可以帮忙吗?
# if the enter is formatted as int plus white space. I think this would solve your problem.
num_str = input() # 1 3 2 1 2
num_str_arr = num_str.split(' ')
num_arr = [int(x) for x in num_str_arr]
print(num_arr)
# output: [1, 3, 2, 1, 2]
# with the list I believe you can do whatever you want.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句