在Python中,编写一个程序,要求用户键入四个数字,并将它们全部放入一个名为ls的列表中

2024-03-29 09:38:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一个程序,要求用户输入如下四个数字:

a = input()
first = int(a) 
b = input() 
second = int(b) 
c = input() 
third = int(c) 
d = input() 
fourth = int(d) 

我不明白我怎么能把它们列在一个单子里。你知道吗


Tags: 用户程序input数字单子intfirstsecond
3条回答

首先,可以使用空行,然后缩进4个空格,将代码放入代码块中。你知道吗

您可以将变量放入如下列表中:

list_variables = [first, second, third, fourth]

请注意,您可以使用列表理解:

lst = [int (input ()) for time in range (4)]

它使用一个简单的for循环执行int (input ())4次。现在您可以使用lst [0]lst [1]'lst [2]lst [3]访问输入。你知道吗

您应该稍微改进一下代码:

first = int(input())
second = int(input())
third = int(input())
fourth = int(input())
lst = [first, second, third, fourth]

相关问题 更多 >