Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PYTHON: Write a function findDigits() that takes a string as a parameter and ret

ID: 3664306 • Letter: P

Question

PYTHON: Write a function findDigits() that takes a string as a parameter and returns a list containing all of the digits that appear in the string. If the string does not contain any digits or an empty string is provided as a parameter, the empty list should be returned. If a digit appears in the string multiple times, it will appear in the list the same number of times. For example:

findDigits(“3”) returns [“3”]

findDigits(“33”) returns [“3”, 3”]

findDigits(“r2d4”) returns [“2”,”4”]

findsDigis(“Radha”) returns [] f

indDigits(“”) returns []

Explanation / Answer

Firstly, you don't want the loop to return True until the end, but you want it to return False and break if the test is false. Your loop returns True or False right on the first iteration, so you don't want the two return statements within the loop. Secondly, you can create a list or string of characters and use it to check whether each character is within that list. In this case, it would be something like ",. " or [",", ".", " "].

You should write something like this:

Here, if the character is not a digit, or the character is not in the list [" ", ".", ","], the function returns False, which automatically ends the loop. If the loop completes without returning a value, then all the characters meet your criteria and the function goes to the next line and returns True.

Edit:

If you want to use a placeholder variable, set it to True and write the loop to set it to False i f the criteria aren't met. If the loop doesn't modify it, the condition is true for the entire string, so it remains True.