Length check with len()

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Length check with len()

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which condition checks that a PIN is exactly 4 characters long?
A pin == 4
B len(pin) == 4
2. A password must have a minimum length of 8 characters. Which check accepts it?
A len(password) >= 8
B len(password) <= 8
3. A username can be at most 12 characters. Which check accepts it?
A len(username) >= 12
B len(username) <= 12
4. A length check tests the...
A number of characters in the input
B meaning of the input
5. Which line is a correct length check in Python?
A if len(code) == 6:
B if len(code) = 6:

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1len(pin) == 4 AChecks that the password is at least 8 characters long
2len(password) >= 8 BUsing > means an 8-character password is rejected. For a minimum of 8 it should be len(password) >= 8.
3Accept a username only if it has no more than 12 characters (maximum length) Cif len(pin) != 4:
4Reject a PIN that is not exactly 4 characters long Dif len(username) <= 12:
5if pin == 4: EChecks that the PIN is exactly 4 characters long
6if len(password) > 8: FThe PIN text is compared directly with the number 4. It should compare its length: len(pin) == 4.

Definitions

len(pin) == 4

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
if ___(pin) == 4:
2.
if len(pin) __ 4:
3.
if len(password) __ 8:
4.
valid = len(username) __ 12
5.
pin = input("Enter 4-digit PIN: ") if len(pin) == _: print("PIN accepted")

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A if len(pin) = 4:
B print('PIN accepted')
C else:
D print('PIN rejected')
Question 2
A pin = input('PIN: ')
B valid = False
C if pin == 4:
D valid = True
E print(valid)
Question 3
A pw = input('Password: ')
B if len(pw) <= 8:
C print('Strong enough')
D else:
E print('Too short')
Question 4
A name = input('Username: ')
B limit = 12
C if len[name] <= limit:
D print('OK')
E else:
F print('Too long')
Question 5
A pin = input('Enter PIN: ')
B while len(pin) != 4:
C print('Must be 4 digits')
D pin == input('Try again: ')
E print('PIN saved')

Answer Key

Length check with len()
Section A: Quick Questions
Q1
B — len(pin) == 4
Q2
A — len(password) >= 8
Q3
B — len(username) <= 12
Q4
A — number of characters in the input
Q5
A — if len(code) == 6:
Section B: Match the Pairs
Q1
len(pin) == 4 → Checks that the PIN is exactly 4 characters long
Q2
len(password) >= 8 → Checks that the password is at least 8 characters long
Q3
Accept a username only if it has no more than 12 characters (maximum length) → if len(username) <= 12:
Q4
Reject a PIN that is not exactly 4 characters long → if len(pin) != 4:
Q5
if pin == 4: → The PIN text is compared directly with the number 4. It should compare its length: len(pin) == 4.
Q6
if len(password) > 8: → Using > means an 8-character password is rejected. For a minimum of 8 it should be len(password) >= 8.
Section C: Definitions
Q1
len(pin) == 4 — A length check compares len() of the text with a required length or limit, such as exactly 4 characters for a PIN, to accept or reject the input.
Section D: Fill in the Blanks
Q1
if len(pin) == 4:
Q2
if len(pin) == 4:
Q3
if len(password) >= 8:
Q4
valid = len(username) <= 12
Q5
pin = input("Enter 4-digit PIN: ") if len(pin) == 4: print("PIN accepted")
Section E: Spot the Error
Q1
A — A single = assigns a value, so it cannot compare the length. A length check needs the comparison operator ==, as in if len(pin) == 4:.
Q2
C — pin is text, so it is never equal to the number 4. The check must compare the length of the text: if len(pin) == 4:.
Q3
B — A minimum length check must accept passwords that are 8 or more characters. <= accepts only short ones, so it should be >=.
Q4
C — len is a function, so it is called with round brackets, not square brackets. It should be len(name).
Q5
D — A double == compares values instead of storing the new input, so pin never changes and a wrong PIN loops forever. It should be a single = to assign: pin = input('Try again: ').