Python realizes the output of multiple integers

Python realizes the output of multiple integers

Programming to output 3 integers

(1) Convert it to an integer first, and then output it into a list through a loop

alist=[]
for i in range(3):
    x = int(input('请输入第{}个整数:'.format(i+1)))#把输入写到循环内,一次一次输入,通过格式化输出
    alist.append(x)
print(alist)
请输入第1个整数:122
请输入第2个整数:34
请输入第3个整数:56
[122, 34, 56]

(2) Enter the string first, split it with a delimiter, and convert it into an integer through a loop

#把输入写在循环外,写一个字符串,通过分隔符分开,循环依次转化为整数
alist=[]
count=input().split(sep=',')
print(count)
for i in count:
    alist.append(int(i))
print(alist)
1,12,123,1234
['1', '12', '123', '1234']
[1, 12, 123, 1234]

Guess you like

Origin blog.csdn.net/weixin_57038791/article/details/129263197