我想绘制此代码但输入
IndexError:列表索引超出范围:k = 浮子(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()
答案 0 :(得分:2)
看起来程序正在等待2个浮点命令行参数。 `
k1 = float(sys.argv[1]) # starting value of K
k2 = float(sys.argv[2])
`
所以你可能会用类似
之类的东西来启动它python main.py 100 200
为了更详细,您的代码正在阅读command line arguments,并且它期望有2个,也可以解析为浮点值。
通常,第一个命令行参数是脚本文件本身,因此sys.argv
始终至少有一个元素长。
话虽如此,您可以按照上面的建议进行操作,并在启动脚本时传递2个浮点数作为参数,或者只编辑脚本并硬编码2个值而不是从命令行读取的值,如此
k1 = 100
k2 = 200