简体   繁体   English

如何在python的列表中显示以下程序的输出?

[英]How to show the output of the following program in a list in python?

numbers=[int(input("Number ( " + str(i+1) + " ) : ")) for i in range(int(input("How many numbers do you want to add ?")))]
for nums in numbers:
    new=[]
    if 10<nums and nums<100:
        new.append(nums)
        print(new)

if you insert 3 numbers like : 124 , 12 , 14 the answer will be : [12] [14] but I want : [12 , 14] thank you :)如果您插入 3 个数字,例如:124、12、14,答案将是:[12] [14] 但我想要:[12, 14] 谢谢:)

numbers = [int(input("Number ( " + str(i + 1) + " ) : ")) for i in
           range(int(input("How many numbers do you want to add ?")))]

new = []  # This must be outside the loop
for nums in numbers:
    if 10 < nums < 100:
        new.append(nums)
print(new)

You're defining the new = [] array inside the for loop, which is causing the creation of a new array every iteration.您在 for 循环内定义new = []数组,这会导致每次迭代都创建一个新数组。

Your code should look like this:您的代码应如下所示:

 numbers=[int(input("Number ( " + str(i+1) + " ) : ")) for i in range(int(input("How many numbers do you want to add ?")))] new = [] for nums in numbers: if 10<nums and nums<100: new.append(nums) print(new)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM