Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right column | |
|---|---|---|
| 1age.isdigit() | Ainput() always returns text (str), so isinstance(age, int) is never True. Use age.isdigit() on the text, or try int(age), instead. | |
| 2isinstance(price, float) | Btry: h = float(text) except ValueError: h = None | |
| 3Test whether text is a whole number by trying to convert it and catching the failure | Ctry: n = int(text) except ValueError: print("Not a whole number") | |
| 4Check that input is a decimal number by trying float() on the text | Disdigit() is False for decimals and negatives because '.' and '-' are not digits. Use try/except around float(temp) to check for a decimal number. | |
| 5age = input("Age: ") if isinstance(age, int): print("Valid") | EReturns True if the value stored in price is already a float | |
| 6temp = "-4.5" if temp.isdigit(): print("Valid number") | FReturns True if the text contains only digits, so it is a safe whole number to convert with int() |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
type check — A check that input is the expected data type, using .isdigit(), try/except around int() or float(), or isinstance(). input() is always text, so check before converting.age = input("Age: ")
if age.isdigit():
age = int(age)try:
n = int(text)
except ValueError:try:
price = float(text)
except ValueError:value = 3.5
print(isinstance(value, float))num = input("Number: ")
print(type(num))