Conditional statements are very vital for obvious reasons For instance, a program might need input and then it would need to analyze the input based on conditions. For instance, if there was some questionnaire about your health, then it might prescribe some meds based on what you said, but I don't think most people would trust a computer.
Diving In
Let's look at some examples. I don't think this is one of the more difficult topics in Python or programming. It's quite obvious. Do you agree or maybe not? Anyway, we will use the "or" operator.
Is all this clear as mud? Notice that equal signs assign things to variables but double equal signs clarify if the stuff on both sides of it are true. For instance, in the first one the double stuff wanted to know if, indeed, your_grade did equal A.
Note: The else statement basically returns the statement it does when you didn't type in A, B, C, or D. It is assumed you would have typed an F, but any letter or letters besides the ones mentioned would have triggered and typing in non-string things, would have just caused an error. Is that last thing true?
Diving In
Let's look at some examples. I don't think this is one of the more difficult topics in Python or programming. It's quite obvious. Do you agree or maybe not? Anyway, we will use the "or" operator.
Code:
your_grade = input("What is your passing grade?")
if your_grade == "A" or your_grade == "a":
print("You probably tried very hard because your grade is" + " " + your_grade + ".")
elif your_grade == "B" or your_grade == "b":
print("You probably tried hard because your grade is" + " " + your_grade + ".")
elif your_grade == "C" or your_grade == "c":
print("You probably didn't try as hard as you could have because your grade is" + " " + your_grade + ".")
elif your_grade == "D" or your_grade == "d":
print("You barely passed because your grade is." + " " + your_grade + ".")
else:
print("Sorry, only passing grades can be analyzed.")
Is all this clear as mud? Notice that equal signs assign things to variables but double equal signs clarify if the stuff on both sides of it are true. For instance, in the first one the double stuff wanted to know if, indeed, your_grade did equal A.
Note: The else statement basically returns the statement it does when you didn't type in A, B, C, or D. It is assumed you would have typed an F, but any letter or letters besides the ones mentioned would have triggered and typing in non-string things, would have just caused an error. Is that last thing true?
Last edited: