cheesecake fun
cheesecake fun

Reputation: 11

When entering a number out of range, python does not give me the right answer

I am working on a simple python exercise, but I cannot get the right answer. Following is the exercise:

Exercise 3: Write a program to prompt for a score between 0.0 and 1.0. If the score is out of range, print an error message. If the score is between 0.0 and 1.0, print a grade using the following table:
Score   Grade
>= 0.9     A
>= 0.8     B
>= 0.7     C
>= 0.6     D
< 0.6    F

Here is my code:

mark = input ('please put in your mark: ')
a=float(mark)
try:
 if 1>=a>=0.9:
   print ('A')
 elif 0.9>a>=0.8:
   print ('B')
 elif 0.8>a>=0.7:
   print ('C')
 elif 0.7>a>=0.6:
   print ('D')
 elif 0.6>a >=0.0:
   print ('F')
except:
 print ('Bad Score')

When I enter a number out of range, it does not give me "Bad Score". Anyone help?

Upvotes: 1

Views: 58

Answers (1)

Simon A
Simon A

Reputation: 221

You need to use else not except

 if 1>=a>=0.9:
   print ('A')
 elif 0.9>a>=0.8:
   print ('B')
 elif 0.8>a>=0.7:
   print ('C')
 elif 0.7>a>=0.6:
   print ('D')
 elif 0.6>a >=0.0:
   print ('F')
 else:
   print('Bad score')

Upvotes: 1

Related Questions