0

For an assignment, I need to use the "len" function to determine how many characters were entered in the "original_word". If the word is greater than or less than five characters the program needs to "exit()"

How can I do this?

original_word: str = input("Enter a 5-character word: ")
character_one: str = input("Enter a single character: ")
print("Searching for " + character_one + " in " + original_word)
if character_one == original_word[0]:
    print(character_one + " found at index 0")
if character_one == original_word[1]:
    print(character_one + " found at index 1")
if character_one == original_word[2]:
    print(character_one + " found at index 2")
if character_one == original_word[3]:
    print(character_one + " found at index 3")
if character_one == original_word[4]:
    print(character_one + " found at index 4")

counter = 0
for c in original_word:
    if c == character_one:
         counter += 1
if counter == 2:
    print(counter, " instances of " + character_one + " found in " + original_word)
if counter == 1:
    print(counter, " instance of " + character_one + " found in " + original_word)
if counter == 0: 
    print("No instances of " + character_one + " found in " + original_word)
kitkat
  • 1

1 Answers1

0

The one of the possible ways is to make the function, which complete the code if the length of your word is 5, else it returns the phrase "Length is not 5, try again"
Also you can do that code a bit shorter. Here is the example:

def main(original_word, character_one):
    if len(original_word) == 5:
        temp_word = original_word
        print(f"Searching for {character_one} in {original_word}")
        while True:
            if character_one in temp_word:
                index = temp_word.find(character_one)
                print(f"{character_one} found at index {index}")
                temp_word = temp_word[:index] + ' ' + temp_word[index + 1:]
            else:
                break

        counter = original_word.count(character_one)
        print(f"{counter} instances of {character_one} found in {original_word}")
    
    else:
        return "The input word is greater or less than 5 characters! Please, try again."


original_word: str = input("Enter a 5-character word: ")
character_one: str = input("Enter a single character: ")

if __name__ == "__main__":
    main(original_word, character_one)

The first modification is f-string. This is the most popular and handy future of concatanation instead of summarizing variables and string.
The second modification is checking if the character is in word, instead of using a lot of if-statements. It is easy to check by:

if x in y:

The way to find the index of character in your string is method find. Sample:

string.find(substring) # It returns the index of substring in string

And the last one is method counter which returns the amount of substring in your string. This is the sample:

string.count(substring) # It returns amount of substring in your string
rockzxm
  • 342
  • 1
  • 9