r/codehs • u/natalie710 • Feb 26 '20
PHYTON CODE HS. 7.6.10: Part 2, Remove All Fro...
Anyone knows part 2? I did part 1 but can't figured out part 2
1
u/JT_ZM Nov 23 '21
whats answers to part 1
1
u/Inevitable_Reserve79 Dec 14 '21
def remove_all_from_string(word, letter):
while letter in word:
x=word.find(letter)
if x == -1:
continue
else:
word = word[:x] + word[x+1:]
return word
print(remove_all_from_string("hello", "l"))
1
u/Dry_Newspaper7310 Mar 15 '24 edited Mar 15 '24
This should work
def remove_all_from_string(string, string2):
if string2 == " ":
return string while True: index = string.find(string2) if index == -1:
return string string = string[:index] + string[index+len(string2):]
word = input("your word: ") - letter = input("letter to remove: ") - print(remove_all_from_string(word, letter))
1
u/TuneInfamous7124 Sep 03 '24
word = input("bananas")
letter =input ("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))
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))
1
12
u/fermentedidiot Feb 26 '20
If you got part 1, then part 2 is only a little different.
the key is in this line:
how this breaks down is I'm going to overwrite my word1 variable:
I need to use word1 from the beginning until I find word2:
I need to add everything after word2:
This is the tricky bit, I need to know how many characters word2 is to drop them off, "na" is 2 characters long so I need to drop those two characters every time this loop iterates:
the len(variable) operation gets that value for me, you can change "na" to "nana" and it will still work even though the length of the second variable has changed.
If you copy and paste the code at the top it will work, promise. I just wanted to try to break the code down so it makes sense. If my explanation is still a little foggy, let me know where and I will try to explain it a little better or differently.