0

So the question I am trying to write code for is this: A student will not be allowed to sit in exam if his/her attendence is less than 75%. Take following input from user Number of classes held Number of classes attended. And print percentage of class attended Is student is allowed to sit in exam or not. Ask user if he/she has medical cause or not ( 'Y' or 'N' ); print accordingly.

Once the user input fails my if condition it is supposed to print a certain message but it just prints something completely different.

I've written an if else statement in case someone hasn't attended 75% of classes. If they haven't the code should ask whether or not they were medically excised. If they were they are allowed to sit the exam but if they responded with anything that isn't a pre-specified "yes" response then they are not allowed to sit the exam. Meaning if they had less than 75% attendence and to the question "are you medically excused" they write "no" or "jk" or "alkdjf" the code should not allow them to sit the exam.

#number of classes held:
total_classes = int(input ("How many clases have been held in the year 
thus far? "));

#number of classes attended:
attended_classes = int(input("How many classes have you attended? "));

#percentage of classes
percentage_attendence = ((attended_classes/total_classes)*100);

if percentage_attendence < 75:
    medic = input("Do you have a medical cause? (Y/N or y/n only)");
    if medic == "y" or "Y" or "yes" or "ye":
        print ("You are free to take part in the exam");
    else:
        print ("You can not take part in the exam");
else:
    print ("You are free to take part in the exam");

The problem is that once I've told the computer that I haven't attended 75% classes, no matter what I answer to the medic question the code still prints I'm allowed to take the exam. If I answer yes, I have the certification, it prints I can take the exam but even if I answer with something that isn't a pre-defined yes in the code python still allows me to take the exam...

Jason Smith
  • 23
  • 2
  • 4

2 Answers2

2

If you want to compare a variable to multiple values you can do this:

if medic in ["y", "Y", "yes", "ye"]:
quamrana
  • 37,849
  • 12
  • 53
  • 71
1

change this line

if medic == "y" or "Y" or "yes" or "ye":

To this

if medic == "y" or medic == "Y" or medic == "yes" or medic == "ye":

This is the output of this change

Pritish kumar
  • 512
  • 6
  • 13
  • This doesn't work for me. If fact this won't work for anyone: `("y" or "Y" or "yes" or "ye")` returns `'y'`. – quamrana Nov 03 '19 at 13:10
  • It doesn't throw anything. Your suggestion is equivalent to `if medic == "y":` – quamrana Nov 03 '19 at 13:14
  • ("y" or "Y" or "yes" or "ye") returns y because y is not False, so when checking this statement from left, it gets true that is "y" so it will return "y". But in this medic == ("y" or "Y" or "yes" or "ye"): it is evaluating medic. – Pritish kumar Nov 03 '19 at 13:15
  • Your edit shows that answering either `'yes'` or `'Y'` to the medical question results in `"You can not take part in the exam"`, which is the wrong branch. – quamrana Nov 03 '19 at 13:21
  • sorry, my mistake didn't see that... Sorry for your time, – Pritish kumar Nov 03 '19 at 13:24
  • This worked for me as well! Thank you! Why didn't what I wrote work tho? – Jason Smith Nov 03 '19 at 15:02