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

Python makes the choice

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

Capital letters count

Computing 8: Topic 1.4, pp. 70–105
GoalCompare strings in a Python if statement, explain why case matters and fix the error caused by comparing text with a number.
New words
string · text in quotation markscase-sensitive · treating capital and small letters as differentTypeError · the message Python gives when a value is the wrong type for an operation
Explanation

Python can compare strings as well as numbers. Strings are compared letter by letter, and capital and small letters are different, so Python treats "Cairo" and "cairo" as different strings. An extra space at the end makes a string different too. That is why the user’s answer must match exactly. Remember that input always returns text. If the program compares that text with a number using > or <, Python stops with a TypeError, which is a runtime error: the program starts, but fails while it runs. The fix is casting with int(). Comparing with == is allowed between text and a number, but it is always False, because text and a number are never equal.

Python
1answer = input("Capital of Egypt? ")
2if answer == "Cairo":
3    print("Correct")
4else:
5    print("Wrong")
Output
Correct
Worked examples
Typedanswer == "Cairo"Printed
CairoTrueCorrect
cairoFalseWrong
CAIROFalseWrong
Cairo FalseWrong
The quiz question program. Cairo matches exactly. Different spellings are all wrong, for the computer.
Python
1age = input("Age? ")
2if age > 12:
3    print("Teen")
Output
TypeError: '>' not supported between instances of 'str' and 'int'
A runtime error. The user types 15, but input gives the text 15, and text cannot be compared with > 12. The program starts, so it is not a syntax error. It stops on line 2. Python shows the last line below.
Python
1age = int(input("Age? "))
2if age > 12:
3    print("Teen")
Output
Teen
The fix is to cast the text to an integer with int(). Now the program compares two numbers. With 15 typed it prints Teen.
Class activity

Match or not (no computer). Write the word Cairo on a card, and prepare ten answer cards with spellings such as Cairo, cairo, CAIRO, Cairo with a space after it, Cario and Cairo. Play the computer: hold up the card with the target, then the answer cards one at a time, and say True or False for answer == "Cairo". Count how many are True. Then invent a new question with your own answer and make ten spellings. Which spellings would you like the computer to accept? Write them down for later. If you have a computer or tablet, run the program with three of your spellings.

Practice
1
Predict the output of the program for the inputs Yes, yes and YES.
Python
1reply = input("Join the club? ")
2if reply == "Yes":
3    print("Welcome")
4else:
5    print("Try again")
2
This program stops with a TypeError when the user types 40. Say which line causes it and rewrite the program correctly.
Python
1price = input("Price? ")
2if price < 50:
3    print("Cheap")
3
Write a Python program that asks for a club word and prints Welcome if the word is exactly friend, and Not this time otherwise.
4
What does this program print, and why?
Python
1print("5" == 5)