while 循环中的数据没有附加到列表中

问题描述 投票:0回答:1

我想编写一个程序,将 while 循环中的数据添加到列表中,并计算数据。但是,当我使用 .append 时,它什么都不返回。

structure = input("Equation: ")
print("RANGE")
upper_lim = int(input("Upper limit: "))
value_i = int(input("The value of 'i': "))
nth = [""]
substitution = [""]

while value_i <= upper_lim and "i" in structure and "+" not in structure:
    substitution = print(structure.replace("i", "(" + str(value_i) + ")"))
    value_i += 1 
    nth.append(substitution)


while value_i <= upper_lim and "i" in structure:
    substitution = print(structure.replace("i", "(" + str(value_i) + ")"))
    value_i += 1
    nth.append(substitution)
    
print(nth)

结果是:

   ['', None, None, None]

而不是像这样的东西:

   [numbers,numbers,numbersr]
python python-3.x list while-loop append
1个回答
0
投票
obj={"one":"1","two":"2"}
while i<5:
    substitution = print(obj) #obj will get printed
    i += 1 
    print("subs = ",substitution) #substitution will have None, since print from the above line will return None
    nth.append(substitution)

Print() 显示项目但返回 None,它保存在替换变量中

© www.soinside.com 2019 - 2024. All rights reserved.