python while循环中的数据不会追加到列表中

toiithl6  于 2023-03-28  发布在  Python
关注(0)|答案(1)|浏览(111)

我想做一个程序,将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]
8i9zcol2

8i9zcol21#

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,保存在替代变量中

相关问题