Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

You must login to ask a question.

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Quizzma Latest Articles

7.6.10 Part 2, Remove All From String CodeHS Answers

The most common answer is:

word = “bananas”
letter = “na”
def remove_all_from_string(word, letter):
while True:
ndex = word.find(letter)
if index == -1:
return word
word = word[:index] + word[index+len(letter):]
print (remove_all_from_string(word, letter))
def find_secret_word(message):
hidden_word = “iCjnyAyT”
for letter in message:
if letter.upper():
hidden_word = hidden_word + letter
return hidden_word
print (find_secret_word(message))

There are a couple of issues in the code snippets.

Firstly, the remove_all_from_string function attempts to remove all instances of a substring (letter) from another string (word). There’s a typo in the variable name (ndex should be index), and the print statement is misplaced.

Secondly, the find_secret_word function seems to be incomplete and unclear in its purpose based on the given code, and the variable message is not defined.

Let’s correct and improve the first function for removing all instances of a substring from another string:

word = "bananas"
letter = "na"

def remove_all_from_string(word, letter):
    while True:
        index = word.find(letter)
        if index == -1:
            return word
        word = word[:index] + word[index+len(letter):]

print(remove_all_from_string(word, letter))

For the second function, it looks like you’re attempting to construct a new string (hidden_word) by adding certain letters from a given message.

However, the condition if letter.upper(): will always be True because letter.upper() returns an uppercase version of the letter, which is a non-empty string (truthy in Python).

You might have intended to filter or process the letters in some specific way, but without further clarification, it’s challenging to correct this function accurately.

Assuming you want to concatenate letters from a message that are uppercase, you could use:

message = "Example Message with a Secret"

def find_secret_word(message):
    hidden_word = ""
    for letter in message:
        if letter.isupper():
            hidden_word += letter
    return hidden_word

print(find_secret_word(message))

This corrected version of the find_secret_word function will go through each letter in message and concatenate it to hidden_word if it’s uppercase.

Please adjust this logic based on your actual requirements if different.

These corrections address the issues in the provided snippets, but please ensure the logic matches your intended outcomes, especially for the second function.

Was this helpful?




Quizzma Team

Quizzma Team

The Quizzma Team is a collective of experienced educators, subject matter experts, and content developers dedicated to providing accurate and high-quality educational resources. With a diverse range of expertise across various subjects, the team collaboratively reviews, creates, and publishes content to aid in learning and self-assessment.
Each piece of content undergoes a rigorous review process to ensure accuracy, relevance, and clarity. The Quizzma Team is committed to fostering a conducive learning environment for individuals and continually strives to provide reliable and valuable educational resources on a wide array of topics. Through collaborative effort and a shared passion for education, the Quizzma Team aims to contribute positively to the broader learning community.

Related Posts

Leave a comment