我想繪製這個代碼,但它進入我得到了一個IndexError:列表索引超出範圍在sys.argv中[1]
IndexError: list index out of range in the line: k = float(sys.argv[1])
誰能幫助我如何解決這一問題?
canWidth=500
canHeight=500
**strong text**
def setupWindow() :
global win, canvas
from tkinter import Tk, Canvas, Frame
win = Tk()
canvas = Canvas(win, height=canHeight, width=canWidth)
f = Frame (win)
canvas.pack()
f.pack()
def startApp() :
global win, canvas
import sys
k1 = float(sys.argv[1]) # starting value of K
k2 = float(sys.argv[2]) # ending value of K
x = .2 # is somewhat arbitrary
vrng = range(200) # We'll do 200 horz steps
for t in range(canWidth) :
win.update()
k = k1 + (k2-k1)*t/canWidth
print ("K = %.04f" % k)
for i in vrng :
p = x*canHeight
canvas.create_line(t,p,t,p+1) # just makes a pixel dot
x = x * (1-x) * k # next x value
if x <=0 or x >= 1.0 :
print ("overflow at k", k)
return
def main() :
setupWindow() # Create Canvas with Frame
startApp() # Start up the display
win.mainloop() # Just wait for user to close graph
if __name__ == "__main__" : main()
您是如何啓動該計劃的? –
你是否用至少兩個命令行參數運行腳本?例如'python3 script.py arg1 arg2' –
如果你用'python filename.py'啓動它,你會得到這個錯誤。例如,如果您使用'python filename.py 1 2'啓動它,則不會出現錯誤。我希望這是明確的原因。 – Michael