Kim Possible
Kim Possible

Reputation: 1

Python exercise 3.3 complete code

I am unable to get the script to return the letter grade. The code allows me to enter the grade, but it does not return the letter grade. It currently is error free, but fails to return response.

#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

Upvotes: 0

Views: 8772

Answers (1)

zephyr
zephyr

Reputation: 2332

The except part of your script only runs if the try part ran into an error. You should have no issues with casting the score to an int so the rest of the script never executes. Why is that in a try-catch block anyway? And why do you want to cast it to an int anyway when it is a number between 0 and 1 and thus not an integer?

You also set your score to 0.0-1.0 which resets it to -1.0, overriding what the user just input. Something like this would work better.

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

Upvotes: 1

Related Questions