Учтите, что hours.get()
вернет строку, а не число.
Умножение строки на число обычно не работает.
>>> 3.7*'4'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'float'
И в случае, если это работает , он не делает то, что вы хотите:
>>> 3*'4'
'444'
Попробуйте вместо этого;
def calculation():
scores = {'A+': 4, 'A': 3.7}
score = scores.get(grade.get(), 1)
try:
num = float(hours.get())
except ValueError: # not a valid float
num = 0
# Create the label when you create the other widgets!
# Just set the contents here.
label['text'] = str(score * num)
Словарь scores
может быть легко расширен для других классов.
Обратите внимание, что я использую здесь метод get
для словаря, потому что он обрабатывает ситуацию, когда ключ отсутствует в словаре. Наблюдать:
>>> scores = {'A+': 4, 'A': 3.7}
>>> scores['A']
3.7
>>> scores['V']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'V'
>>> scores.get('V', 42)
42