简体   繁体   English

使用Python创建程序:程序必须计算单词数并计算每个单词中的字母数

[英]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). 例如:糖果是红色的(输出必须是: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' 使用无限的while循环,并在用户输入“ 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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