jeudi 20 février 2020

Functions with If

Question: How can I update the my code so that when I type "perfect" as a score, it returns with the words, "bad score?" In trying to do so in my current code, I get the following message:

Traceback (most recent call last): File "/Users/renelyncruz/Desktop/Bellevue College Work/CS 101/Cruz_FunctionsWithIf.py", line 1, in grade_rate=float(input("Please enter a score between 0.0 and 1.0.")) ValueError: could not convert string to float: ' perfect'

My current code is as follows:

grade_rate=float(input("Please enter a score between 0.0 and 1.0."))

def computegrade(grade_rate):

if grade_rate<0.0 or grade_rate>1.0:
    return ('Bad score')

elif grade_rate>=0.9:
    return ("Your grade would be an A! Congratulations!")

elif grade_rate>=0.8:
    return ("Your grade would be a B! Congratulations!")

elif grade_rate>=0.7:
    return ("Your grade would be a C. You should try harder.")

elif grade_rate>=0.6:
    return ("Your grade would be a D. Keep trying!")

elif grade_rate<0.6:
    return ("Your grade would be an F. Work harder!")

else :
    return ('Bad score')

try: grade=computegrade(grade_rate) print(grade)

except ValueError as e: print("Bad Score")

Aucun commentaire:

Enregistrer un commentaire