我剛剛開始學習python,任何人都可以幫助我。 例如,如果我有一個名爲'速度'的變量,我只希望它達到100而不是0以上,而不是0以下。但是我還希望代碼仍然運行,以便我可以將它設置得更低或更高,我的代碼到目前爲止:Python - 設置變量的限制
import tkinter as tk
speed = 80
def onKeyPress(event, value):
global speed
text.delete("%s-1c" % 'insert', 'insert')
text.insert('end', 'Current Speed: %s\n\n' % (speed,))
speed += value
print(speed)
if speed >= 100:
text.insert('end', 'You have reached the speed limit')
speed = 80
root = tk.Tk()
root.geometry('300x200')
text = tk.Text(root, background='black', foreground='white', font=('Comic Sans MS', 12))
text.pack()
# Individual key bindings
root.bind('<KeyPress-w>', lambda e: onKeyPress(e, 1))
root.bind('<KeyPress-s>', lambda e: onKeyPress(e, -1)) #
root.mainloop()
我怎樣才能得到'速度'變量停在100沒有停止整個代碼?
不知道爲什麼-1 ...這是一個很好的答案imho .. +1 –