这是我的程序。一切正常。问题是,我需要用户能够确定循环的数量。我尝试过的所有方法都不起作用。实现Python动态循环?
这是代码:
def main():
print("This is a chaotic function")
# c voor index, a & b zijn user inputs
a = float(input("Number between 0 and 1: ").replace(',','.'))
b = float(input("Another number between 0 and 1: ").replace(',','.'))
c = int(input("How many iterations? "))
z = 0
# print de randen van de tabellen
print('%10s %20s %20s' % ("Index", a, b))
print("______________________________________________________")
a_temp = a
b_temp = b
# loops berekenen de chaotische nummers voor de input
for i in range(10):
a_temp = 3.9 * a_temp * (1 - a_temp)
b_temp = 3.9 * b_temp * (1 - b_temp)
z = z + 1
#Print resultaten naar tabel
print('%10s %20s %20s' % (c, a_temp, b_temp))
main()
的一段代码我试图执行:
for i in range (c)
不工作。我将如何得到这个工作?
当你在范围(c)中使用'for i时,你会得到什么错误? – user3557327 2014-09-26 21:14:48
它不会给出错误,但它只是迭代一次,而不管int C是什么。 C = 23迭代一次,C = 5迭代一次。我在范围(10)迭代10次。 – Menubalk 2014-09-26 21:16:56
只打印一次,如果这就是你的意思。 – jonrsharpe 2014-09-26 21:18:12