2017-03-03 78 views
0

我知道這個問題已被多次詢問,但我仍然無法在我的代碼中找到問題。我的程序是一個等級計算器。輸入成績後,我收到此錯誤:TypeError:無法乘以'float'類型的非int序列 - 在轉換爲浮點數後

Traceback (most recent call last): 
    File "/Users/Jeremy/Documents/Python Projects/Mosier_Jeremy_HW4.py", line 59, in <module> 
    main() 
    File "/Users/Jeremy/Documents/Python Projects/Mosier_Jeremy_HW4.py", line 53, in main 
    total = calcTotal (entry_exam1, entry_exam2, entry_exam3, entry_hw, entry_lqr, entry_fp) 
    File "/Users/Jeremy/Documents/Python Projects/Mosier_Jeremy_HW4.py", line 29, in calcTotal 
    total = float ((exam1 * EXAM1_WEIGHT) + (exam2 * EXAM2_WEIGHT) + (exam3 * EXAM3_WEIGHT) + (hw * HW_WEIGHT) + (lqr * LQR_WEIGHT) + (fp * FP_WEIGHT)) 
TypeError: can't multiply sequence by non-int of type 'float' 

這裏是我的代碼:

EXAM1_WEIGHT = .2 
EXAM2_WEIGHT = .2 
EXAM3_WEIGHT = .2 
HW_WEIGHT = .2 
LQR_WEIGHT = .1 
FP_WEIGHT = .1 

def entry_validation (assignment) : 
    entry = -1 

    while entry == -1: 
     entry = input ('What was your final score for ' + assignment + '? ') 

     if entry == '': 
      entry = -1 
      print ('ERROR: You cannot leave this field blank. Please try again.') 

     else: 
      entry = float (entry) 

      if entry < 0 or entry > 100: 
       entry = -1 
       print ('ERROR: You score must be between 0 and 100. Please try again.') 

    return assignment 


def calcTotal (exam1, exam2, exam3, hw, lqr, fp) : 
    total = float ((exam1 * EXAM1_WEIGHT) + (exam2 * EXAM2_WEIGHT) + (exam3 * EXAM3_WEIGHT) + (hw * HW_WEIGHT) + (lqr * LQR_WEIGHT) + (fp * FP_WEIGHT)) 

    return total 

def calcLetter (total) : 
    if total < 89.5: 
     return 'A' 
    elif total < 79.5: 
     return 'B' 
    elif total < 69.5: 
     return 'C' 
    elif total < 59.5: 
     return 'D' 
    else: 
     return 'F' 

def main() : 
    entry_exam1 = entry_validation ('Exam 1') 
    entry_exam2 = entry_validation ('Exam 2') 
    entry_exam3 = entry_validation ('Exam 3') 
    entry_hw = entry_validation ('Homework') 
    entry_lqr = entry_validation ('Language Quick Reference') 
    entry_fp = entry_validation ('Final Project') 

    total = calcTotal (entry_exam1, entry_exam2, entry_exam3, entry_hw, entry_lqr, entry_fp) 
    letter = calcLetter (total) 

    print ('Your total score is a(n): ', format (total, ',.2%')) 
    print ('Your final letter grade is a(n): ', letter) 

main() 

回答

0

從你的代碼,我認爲你要entry_validation返回浮點型,但它實際上是返回字符串你提供時調用它。

例如,在:

entry_exam1 = entry_validation ('Exam 1') 

entry_validation ('Exam 1')將返回字符串Exam 1。如果您嘗試通過任何浮點數乘以一個字符串,則會得到您報告的TypeError

你將不得不修改entry_validation功能,使其返回float:

def entry_validation (assignment) : 
    entry = -1 

    while entry == -1: 
     entry = input ('What was your final score for ' + assignment + '? ') 

     if entry == '': 
      entry = -1 
      print ('ERROR: You cannot leave this field blank. Please try again.') 

     else: 
      # Let's handle possible exceptions when the input cannot be converted 
      try: 
       entry = float (entry) 
       if entry < 0 or entry > 100: 
        entry = -1 
        print ('ERROR: You score must be between 0 and 100. Please try again.') 
       else: 
        return entry 
      except ValueError: 
       entry = -1 
       print("ERROR: Must be a float") 
+0

謝謝!這是問題。程序現在工作很好。 –

相關問題