options = [ "Choice 1: 1-9", "Choice 2: 10- 100", ]
button = buttonbox ( msg = "Choose your range of numbers", title = "Select number range", choices = options)
if button == "Choice 1: 1-9":
from random import randint
rn1 = randint(30,50)
from random import randint
rn2 = randint(30,50)
options = [ "Close"]
buttonbox ( msg = "%s X %s = ___" % (rn1, rn2), title = "Select number range", choices = options)
if button == "Choice 2: 10- 100":
from random import randint
rn2 = randint (10,100)
from random import randint
rn2 = randint (10,100)
options = [ "Close"]
buttonbox ( msg = "%s X %s = ___" % (rn1, rn2), title = "Select number range", choices = options)这是我的代码,当我尝试运行第二个选项时,它表示NameError: name 'randint' is not defined,尽管代码与第一个选项完全相同,但是数字不同。
有什么建议吗?
发布于 2015-04-09 14:59:50
只导入一次,在文件的顶部。将from random import randint放在程序的顶部,并在所有其他地方删除它。我想这就是你想要的:
from random import randint
options = ["Choice 1: 1-9", "Choice 2: 10- 100"]
button = buttonbox(msg="Choose your range of numbers", title="Select number range", choices=options)
if button == options[0]:
rn1 = randint(30, 50)
rn2 = randint(30, 50)
elif button == options[1]:
rn1 = randint(10, 100)
rn2 = randint(10, 100)
options = ["Close"]
buttonbox (msg="%s X %s = ___" % (rn1, rn2), title="Select number range", choices=options)https://stackoverflow.com/questions/29534724
复制相似问题