AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Words that join conditions

Week 8 · 1.4 Conditional statements in text-based programming · 3 lessons
2

Many spellings, one meaning

Computing 8: Topic 1.4, pp. 70–105
GoalUse or to accept several spellings of one answer and avoid the shortcut that does not work.
New words
spelling · the exact letters and capitals of a typed answeraccept · let the program treat an answer as correctcomplete condition · a condition with a value, an operator and a value, such as answer == "yes"
Explanation

A user may type yes, Yes or YES, and all three mean the same to a person. Python is case-sensitive, so it needs to be told. The way to do it with the tools of this course is to join one complete condition for each spelling with or. If any one of them matches, the whole condition is true. It is tempting to shorten it to answer == "yes" or "Yes", but this does not do what it seems. Python reads "Yes" alone as a second condition, and a text that is not empty counts as true. So the shortened version is true for every answer. You will meet the reason in the next topic. Always write the whole condition for each spelling.

Python
1answer = input("Snack? ")
2if answer == "yes" or answer == "Yes" or answer == "YES":
3    print("Here you are")
4else:
5    print("Maybe later")
Output
Here you are
Worked examples
TypedPrinted
yesHere you are
YesHere you are
YESHere you are
yEsMaybe later
noMaybe later
The snack program accepts three spellings. Each one is a separate complete condition. Any other word goes to the else.
Python
1answer = input("Snack? ")
2if answer == "yes" or "Yes":
3    print("Here you are")
4else:
5    print("Maybe later")
Output
Here you are
The shortcut is a trap. The program below runs without an error, but it prints Here you are for any answer, even maybe. This is a logic error.
VersionConditionAnswer maybe gives
longanswer == "yes" or answer == "Yes"Maybe later
shortcutanswer == "yes" or "Yes"Here you are (wrong)
A table of the two versions for the answer maybe. Only the long version is correct.
Class activity

Spelling cards (no computer). Write a quiz answer on a card, for example the word Delta. Make eight answer cards with different spellings: delta, Delta, DELTA, DeLTa, Delt, Delta with an extra space, D and Delta. For each card write True or False for the condition answer == "Delta" or answer == "delta". Which cards are True? How many conditions would you need to add to also accept DELTA? Write the complete condition. If you have a computer or tablet, run the program with three of your spellings.

Practice
1
Write a Python program that asks Ready? and prints Go for no, No or NO, and Wait for any other answer.
2
Predict what the program prints for the inputs blue, Blue and BLUE.
Python
1colour = input("Colour? ")
2if colour == "blue" or colour == "Blue":
3    print("Accepted")
4else:
5    print("Rejected")
3
A pupil says: I wrote if reply == "stop" or "Stop": and my program always stops. Explain what happened.
4
Why do we use or and not and when we accept several spellings?