2016-03-23 5 views
0

文字のグレードを返すスクリプトを取得できません。このコードでは、学年に入ることができますが、文字の等級は返されません。現在のところエラーはありませんが、応答は返されません。 try部分がエラーに遭遇した場合、スクリプトのPythonエクササイズ3.3完全コード

#Given (1) score, and (2) grade 
# For scores between 0.0 - 1.0, this programs prints the letter grade 
# For scores enter out of the range of 0.0- 1.0 this program will print an error message 
# Use try/catch exception handling to gracefully exit on values outside of the specified range 

import sys 


score= input ("Enter numeric value of score: ") 
score = 0.0-1.0 
# Convert input from default string value to integer 
floatScore = float (score) 
try: 
    intScore = int (score) 


except: 
    if score > 1.0: 
     print ("Bad Score") 


# Use conditional loop to display letter grade based on user-supplied score 


# Print letter grade 


    elif 1.0 >= score>=.9: 
     print ("Grade is A" + str(intScore)) 
    elif .9 > score>=.8: 
     print ("B") 
    elif .8 >score>=.7: 
     print ("C")  
    elif .7 >score>=.6: 
     print ("D") 
    elif .6 >score>=.5: 
     print ("F") 

# End program 
+1

この行: 'score = 0.0-1.0'は、あなたが' input'から得たものを負のもので上書きします。 – L3viathan

+0

'score = float(input("スコアの数値を入力: ")) - 1.0' – Bahrom

答えて

1

except部分にのみ実行されます。スコアをintにキャストしてスクリプトの残りの部分を実行しないようにするには、問題はありません。とにかくtry-catchブロックでそれがなぜですか?そして、なぜそれが0と1の間の数で整数ではないので、それをintにキャストしたいのですか?

スコアを0.0〜1.0に設定して、ユーザーが入力した内容を上書きして-1.0にリセットします。このようなものがうまくいくでしょう。

import sys 

score= input ("Enter numeric value of score: ") 

try: 
    score = float(score) 

    if score > 1.0: 
     print ("Bad Score") 

    # Use conditional loop to display letter grade based on user-supplied score 

    # Print letter grade 
    elif 1.0 >= score>=.9: 
     print ("Grade is A" + str(score)) 
    elif .9 > score>=.8: 
     print ("B") 
    elif .8 >score>=.7: 
     print ("C")  
    elif .7 >score>=.6: 
     print ("D") 
    elif .6 >score>=.5: 
     print ("F") 

except ValueError: 
    print("You need to input a number") 

# End program 
+0

提案していただきありがとうございます...スクリプトは文字のグレードを返しますスコアの数値を入力してください:.80 トレースバックファイル "C:/ Users/Kim/Documents // help 3.py"、行2、 intScore = int(score) ValueError:ベース10のint()のリテラルが無効です: '。 80 ' >>> –

+0

これにtry-catchステートメントがどのように適用されるのかわかりません。どのようなエラーがキャッチされるのですか?有効範囲外に何かを入力しても、キャッチするエラーは発生しません。 – zephyr

+0

浮動小数点(スコア)の代わりにint(スコア)を使用している人が1.0 –

関連する問題