The Red
The Red

Reputation: 5

List in if statement python 3

gradeOutput= ['A','B','C','D','E','F']

#USE OF IF STATEMENT TO SORT USER DATA
if ((grade== gradeOutput[0]) or (eco >= 85)):
   print("Great!, you have met the highest criteria")
elif ((grade== gradeOutput[0:3]) and (health >= 60) and (eco >= 60)):
  print("Congratulations you have met the criteria")
else :
  print("Apologises, you have not met the criteria")

I am current doing a basic task for my coursework and I'm stuck and I'm not sure why.

I have created my list and using the 0,1,2,3,4 rule. I have asked in the elif for the grade to be between A-C, economic status to be a minimum of 60, and the health score to be minimum of 60 to access onwards but when I insert the correct details such as, Grade C, 70 Economic status and 70 health it gives me the else statement which stops the program.

Any help would be grateful.

Upvotes: 0

Views: 66

Answers (2)

AMC
AMC

Reputation: 143

In your elif statement you are comparing a single item (grade) to a list of items (['B', 'C', 'D']). You need to change to:

elif grade in gradeOutput2[0:2] and health >= 60 and eco >= 60:         
    print("Congratulations you have met the criteria")

Upvotes: 1

Josh Hamet
Josh Hamet

Reputation: 957

grade == gradeOutput2[0:2]

I believe you intend to check if grade is within that slice. Therefore, use the syntactic sugar of in to check if your element is within that slice, NOT if it is equivalent.

grade in gradeOutput2[0:2]

Upvotes: 0

Related Questions