我有一个简单的python(版本2.7.3)代码,有一个我无法弄清楚的输出.代码提示用户输入分数(如果输入不是0到1之间的数字,则会继续这样做),确定字母等级,然后退出.代码如下:
def calc_grade():
try:
score = float(raw_input("Enter a score: "))
if score > 1.0:
print "Error: Score cannot be greater than 1."
calc_grade()
except:
print "Error: Score must be a numeric value from 0 to 1."
calc_grade()
print "\nthe score is: %s" % (score)
if score >= 0.9:
print "A"
elif score >= 0.8:
print "B"
elif score >= 0.7:
print "C"
elif score >= 0.6:
print "D"
else:
print "F"
return 0
calc_grade()
Run Code Online (Sandbox Code Playgroud)
如果我运行此脚本,请尝试输入:1.5,h,0.8,然后我得到以下输出:
Enter a score: 1.5
Error: …Run Code Online (Sandbox Code Playgroud) 我喜欢明确指定参数。例如:
def func(a, b):
return a+b
Run Code Online (Sandbox Code Playgroud)
每当我调用它时,我都会写:
func(a=6, b=7)
Run Code Online (Sandbox Code Playgroud)
代替:
func(6, 7)
Run Code Online (Sandbox Code Playgroud)
我不能用OptionMenuTkinter 中的类来做到这一点。以下示例位于自定义类中
self.var = tk.StringVav()
choices = ['op1', 'op2']
self.menu_m = tk.OptionMenu(master=self.frame_1, variable=self.var, *choices)
Run Code Online (Sandbox Code Playgroud)
这导致参数“master”有多个值。如何显式定义要使用的主选项、变量和选项列表?