Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The following function should return a copy of the input string with all digits removed. However it contains a logic error. What's wrong, and how

The following function should return a copy of the input string with all digits removed. However it contains a logic error. What's wrong, and how would you fix it?
def remove_digits(s):
new_s =""
for letter in s:
if letter.isdigit():
new_s = new_s + letter
return new_s
Group of answer choices
The code adds characters if they are digits, rather than if they're not digits. The condition should be reversed.
def remove_digits(s):
new_s =""
for letter in s:
if not letter.isdigit():
new_s = new_s + letter
return new_s
new_s should be reassigned to the matching letter each time the loop finds a digit.
def remove_digits(s):
new_s =""
for letter in s:
if letter.isdigit():
new_s = letter
return new_s
The string concatenation for new_s is backwards. It should be
def remove_digits(s):
new_s =""
for letter in s:
if letter.isdigit():
new_s = letter + new_s
return new_s
There should not be a new_s, instead modify s directly.
def remove_digits(s):
for letter in s:
if letter.isdigit():
s = s + letter
return s

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Explain in detail the different methods of performance appraisal .

Answered: 1 week ago

Question

5. Understand how cultural values influence conflict behavior.

Answered: 1 week ago

Question

e. What do you know about your ethnic background?

Answered: 1 week ago