Write a function that takes, as an argument, a list, identified by the variable
ID: 3757224 • Letter: W
Question
Write a function that takes, as an argument, a list, identified by the variable aList. If the list only contains elements containing digits (either as strings as integers), return the string formed by concatenating all of the elements in the list (see the example that follows). Otherwise, return a string indicating the length of the list, as specified in the examples that follow. Name this function AmIDigits(aList). For example, >>> AmIDigits([“hello”, 23]) should return the string “The length of the input is 2.” As another example, >>> AmIDigits ([“10”, “111”]) should return the string “10111” As a final example, >>> AmIDigits ([“123”, 4, 5, 6, 7]) should return “1234567” Use Python
Explanation / Answer
def AmIDigits(aList): result = '' for item in aList: item = str(item) for c in item: if c.isdigit(): result += c else: return "The length of the input is " + str(len(aList)) + "." return result print(AmIDigits(["hello", 23])) print(AmIDigits (["10", "111"])) print(AmIDigits (["123", 4, 5, 6, 7]))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.