Python Forum

Full Version: How to Continuously Remove Letters from Words
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to write a program which prompts the user for a word. Then, the program will ask what letter should be removed, only allowing one letter at a time, and how many times they want to continue this removing process, beforehand. My code works for the first round where it successfully removes that letter. But afterwards, it goes a bit haywire and I have no clue why. I'm wondering if there are any noticeable bugs and what I can do to get it to work successfully. If someone can help me figure this out, that would be great...

new_word = ""

word = str(input("Please enter a word: "))
removed_letter = str(input("Which letter do you want to see removed?: "))

while len(removed_letter) > 1:
    print("Only one letter at a time.")
    removed_letter = str(input("\nWhich letter do you want to see removed?: "))

process_repeat = int(input("How many times do you want to repeat this process?: "))

while process_repeat > len(word):
    print("You don't even have that many letters in your word")
    process_repeat = int(input("\nHow many times do you want to repeat this process?: "))

while len(removed_letter) > 1:
    print("Only one letter at a time.")
    word = str(input("Please enter a word: "))
    removed_letter = str(input("Which letter do you want to see removed?: "))


for num in range(process_repeat):
    for num in range(len(word)):
        if word[num] == removed_letter:
            new_word += ""
            
        else:
            new_word += word[num]
    print(new_word)
    word = new_word
    removed_letter = str(input("\nWhich letter do you want to see removed?: "))
Input returns string, so you don't need to use str.

You should think about logic - what will happen if user enter letter which is not in the word; if number of repetitions is higher than number of letters in word?

Conditions should be established before while loop. One possibility:

- ask for word
- ask for number of repetitions (NB! think about datatype)
- while repetition is greater than 0: do your thing; decrease repetition by one