我試圖通過努力工作學習Python的艱辛之路,並試圖在我可以推進我的教育的地方混亂。我認爲這會起作用:設置raw_input爲while循環設置一個限制,然後讓while循環執行到我用變量「frequency」建立的限制。它不是,呃,不是。相反,它將Python發送到創建數字列表的無限循環中,每個列表看起來都比最後(非常長)的列表長一個增量。爲什麼raw_input在這個Learn Python Hard Way練習變體中創建一個無限循環?
原始代碼有「而我< 6」 - 我已經添加了raw_input變量,並且即使我輸入與原始代碼(6)中相同的變量,它也會執行此無限循環。
以下是完整的腳本:
i = 0
numbers = []
print "What is the frequency?"
frequency = raw_input ('Kenneth? ')
while i < frequency:
print "At the top i is %d" % i
numbers.append(i)
i = i + 1
print "Numbers now: ", numbers
print "At the bottom i is %d" % i
print "The numbers: "
for num in numbers:
print num
道歉最大的無知 - 我在平裝我面前了Python中的副本,軟件設計,現在,它在我的下一件事 - LPTHW後列出。
您可能會發現下面的有用的前只需添加
frequency = int(frequency)
。這是關於如何在Python中比較字符串和整數。 http://stackoverflow.com/questions/3270680/how-does-python-compare-string-and-int – Dunes 2011-12-30 22:12:31嘗試將'raw_input'改爲'input' – 2011-12-30 22:15:36
或'frequency = int(raw_input('Kenneth?')) ' – 2011-12-30 22:18:10