Capital letters count
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.
1answer = input("Capital of Egypt? ") 2if answer == "Cairo": 3 print("Correct") 4else: 5 print("Wrong")
Correct
| Typed | answer == "Cairo" | Printed |
|---|---|---|
| Cairo | True | Correct |
| cairo | False | Wrong |
| CAIRO | False | Wrong |
| Cairo | False | Wrong |
1age = input("Age? ") 2if age > 12: 3 print("Teen")
TypeError: '>' not supported between instances of 'str' and 'int'
1age = int(input("Age? ")) 2if age > 12: 3 print("Teen")
Teen
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.
1reply = input("Join the club? ") 2if reply == "Yes": 3 print("Welcome") 4else: 5 print("Try again")
1price = input("Price? ") 2if price < 50: 3 print("Cheap")
1print("5" == 5)