简体   繁体   中英

Create a Program using Python: Program must count the number of words and count the number of letters in each word

The difficult part for me is the letter counting, It needs to be like this. ex: The candy is red (output needs to be: 3 5 2 3). Also the current code takes spaces into consideration which it should not.

This is what I have so far:

def main():
    phrase = input("Enter Your Sentence:")
    words = phrase.split()
    WordCount = len(words)
    LetterCount = len(phrase)
    print("Total Amount of Words in Sentence: %s" % WordCount)
    print("Total Amount of Letters in Sentence: %s" % LetterCount)
main()

example:

Enter your sentence: The sky is blue  
Total amount of words: 4 
Total amount of letters: 3 3 2 4

Try this:

def main():
    phrase = input("Enter Your Sentence: ")
    return [len(item) for item in phrase.split()]

>>> main()
Enter Your Sentence: The candy is red
[3, 5, 2, 3]
>>> main()
Enter Your Sentence: These are words
[5, 3, 5]
>>> 
def main():
        phrase = input("Enter Your Sentence:")
        words = phrase.split()
        WordCount = len(words)
        LetterCount = [len(word) for word in words]
        print("Total Amount of Words in Sentence:", WordCount)
        print("Total Amount of Letters in Sentence:", LetterCount)

main()

use an infinite while loop and break out of the loop when the user enters 'quit'

def main():
    while True:
        phrase = input("Enter Your Sentence or quit to exit: \n")
        if phrase.lower() == 'quit':
            break
        else:
            words = phrase.split()
            WordCount = len(words)
            LetterCount = [len(word) for word in words]
            print("Total Amount of Words in Sentence:", WordCount)
            print("Total Amount of Letters in Sentence:", LetterCount)

main()
def user_input():
    phrase = input("Enter your sentence: ")
    return phrase

def word_count(phrase):
    list_of_words = phrase.split()
    count = 0
    for word in list_of_words:
        count += 1
    return count

def letter_count(phrase):
    list_of_words = phrase.split()
    count = 0
    letter_list = []
    for word in list_of_words:
        for char in word:
            count += 1
        letter_list += [count]
        count = 0
    letter_string = ' '.join(map(str, letter_list))
    return letter_string

def restart_program():
    restart_question = "Would you like to restart the program (y or n)? "
    restart_answer = str(input(restart_question.lower()))
    return restart_answer

def main():
    start_again = "y"
    while start_again == "y":
        phrase = user_input()
        number_words = word_count(phrase)
        letters = letter_count(phrase)

        print ("Total amount of words: ", number_words)
        print ("Total amount of letters: ", letters)
        print ()

        start_again = restart_program()

        print ()

    print ("Good Bye")

main()

Example Output:

Enter your sentence: The sky is blue
Total amount of words:  4
Total amount of letters:  3 3 2 4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM