Python Forum
How to Continuously Remove Letters from Words
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Continuously Remove Letters from Words
#1
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?: "))
Reply
#2
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to use asyncio run_forever to continuously handle requests mansky 1 664 Oct-28-2023, 04:26 AM
Last Post: deanhystad
  How to continuously receive messages until specified to stop using Bleak jacobbreen25 3 2,119 Dec-28-2022, 04:25 PM
Last Post: jacobbreen25
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,792 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  Replacing a words' letters in a string cananb 2 3,456 Dec-01-2020, 06:33 PM
Last Post: perfringo
  How to listen to clipboard content change without polling the clipboard continuously? edgelord 0 2,475 Nov-27-2020, 06:07 AM
Last Post: edgelord
  check if letters in a words Vimart 4 5,490 Aug-10-2020, 09:59 PM
Last Post: deanhystad
  item from a line to list however when i print the line instead of words i get letters Sutsro 5 2,951 Apr-22-2020, 02:39 PM
Last Post: deanhystad
  Custom logging handler looping continuously linuxaddikt 0 1,785 Mar-12-2020, 06:58 PM
Last Post: linuxaddikt
  Continuously iterating through a csv file ExplodingCoyote 2 3,665 Feb-26-2020, 07:56 AM
Last Post: DeaD_EyE
  Compare all words in input() to all words in file Trianne 1 2,765 Oct-05-2018, 06:27 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020