Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
Write add(name, number) for the contact book. It must refuse a number with letters or an empty number. What happens for "98765", "98a76" and ""?
isdigit() is True only for a non-empty text of digits.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, compare your working with the solution.
The check is number.isdigit(). It is True only if the text is not empty and every character is a digit.
"98765": all digits, so it is added."98a76": the a is not a digit, so ValueError is raised, and the book refuses it."": an empty text is not made of digits, so it is refused as well.The main program catches the ValueError with except and prints the reason.
Log in to join the discussion.
Loading…