存储并 return 同一变量的先前值
Store and return a previous value for same variable
我正在尝试存储变量的先前值,该变量已通过对话框更改,然后如果对话框被取消并且 returns,则将该变量的值转换回先前设置的值None
。例如,考虑:
letter_height = 30
letter_width = 50
我希望用户通过这个改变值,这确实有效:
def NewLetterDimensions():
global letter_height
letter_height = (numinput("New Letter Height", "Please set the new letter height: ", minval = 10, maxval = 170))
if letter_height == None:
listen()
global letter_width
letter_width = (numinput("New Letter Width", "Please set the new letter width: ", minval = 10, maxval = 170))
if letter_width == None:
listen()
调用的是:
onkey(NewLetterDimensions, "k")
但是,如果对话框被取消,我希望 "if" 语句能够 return 存储的先前值,以便将变量更改为先前值而不是 None
。那么,我将如何着手实施呢?所以,我想要这个:
def NewLetterDimensions():
global letter_height
letter_height = (numinput("New Letter Height", "Please set the new letter height: ", minval = 10, maxval = 170))
if letter_height == None:
[] <- # Return previously set letter height (whether from previous dialog or not)
listen()
global letter_width
letter_width = (numinput("New Letter Width", "Please set the new letter width: ", minval = 10, maxval = 170))
if letter_width == None:
[] <- # Return previously set letter width (whether from previous dialog or not)
listen()
是的,这是海龟图形。
怎么样:
user_input = (numinput("New Letter Height", "Please set the new letter height: ",
minval=10, maxval=170))
letter_height = letter_height if user_input is None else user_input
仅当 letter_height
不是 None
时才将 letter_height
更改为 user_input
,否则保留旧值。
顺便说一句,总是将 None
与 is
测试进行比较,因为 PEP8 建议:
Comparisons to singletons like None
should always be done with is
or is not
, never the equality operators.
我正在尝试存储变量的先前值,该变量已通过对话框更改,然后如果对话框被取消并且 returns,则将该变量的值转换回先前设置的值None
。例如,考虑:
letter_height = 30 letter_width = 50
我希望用户通过这个改变值,这确实有效:
def NewLetterDimensions():
global letter_height
letter_height = (numinput("New Letter Height", "Please set the new letter height: ", minval = 10, maxval = 170))
if letter_height == None:
listen()
global letter_width
letter_width = (numinput("New Letter Width", "Please set the new letter width: ", minval = 10, maxval = 170))
if letter_width == None:
listen()
调用的是:
onkey(NewLetterDimensions, "k")
但是,如果对话框被取消,我希望 "if" 语句能够 return 存储的先前值,以便将变量更改为先前值而不是 None
。那么,我将如何着手实施呢?所以,我想要这个:
def NewLetterDimensions():
global letter_height
letter_height = (numinput("New Letter Height", "Please set the new letter height: ", minval = 10, maxval = 170))
if letter_height == None:
[] <- # Return previously set letter height (whether from previous dialog or not)
listen()
global letter_width
letter_width = (numinput("New Letter Width", "Please set the new letter width: ", minval = 10, maxval = 170))
if letter_width == None:
[] <- # Return previously set letter width (whether from previous dialog or not)
listen()
是的,这是海龟图形。
怎么样:
user_input = (numinput("New Letter Height", "Please set the new letter height: ",
minval=10, maxval=170))
letter_height = letter_height if user_input is None else user_input
仅当 letter_height
不是 None
时才将 letter_height
更改为 user_input
,否则保留旧值。
顺便说一句,总是将 None
与 is
测试进行比较,因为 PEP8 建议:
Comparisons to singletons like
None
should always be done withis
oris not
, never the equality operators.