有没有办法将输入转换为列表?

DrD*_*ive 3 python input list python-3.x

我正在尝试制作一个普通计算器,以便您可以输入任意数量的数字,我正在尝试将输入转换为列表,我该怎么做?这是我到目前为止的代码。

numbers = []
divisor = 0
total = 0

adtonum = int(input("Enter numbers, seperated by commas (,): "))
numbers.append(adtonum)

for num in numbers:
    divisor += 1
    total = total + num
    print(num)

print("Average: ")
print(total / divisor)
Run Code Online (Sandbox Code Playgroud)

cos*_*ras 6

尝试这个:

adtonum = input("Enter numbers, separated by commas (,): ")
numbers = [int(n) for n in adtonum.split(',')]
Run Code Online (Sandbox Code Playgroud)

在这里,我们通过分隔符(在本例中为逗号)拆分该行,并使用列表理解来构建数字列表——将输入字符串中的每个数字一一转换为整数。