回到我的if语句

时间:2017-07-04 22:31:54

标签: python-3.x

这是我第一次访问本网站并且是编程新手。我需要用户能够输入另一个单词,如果他们说" y"。截至目前,该程序将它们发送回 语句。任何建议将不胜感激。

print('Welcome to Word Madness!!')

vowels = list('aeioyu')

consonants = list('bcdfghjklmnpqrstvwxz')

wordCount = 0

complete = False

while not complete:
    mode = input('Would you like to type Vowels, Consonants, or Quit?: ').lower().strip()
    print('You chose to enter: ',str(mode))

#When user chooses to quit program will system exit

    if mode == 'quit':
        print('Sorry to see you go! Come back to Word Madness soon!')
        import sys
        sys.exit(0)

#If vowels are selected then they will be counted

    if mode == 'vowels':
        word = input('Please enter your word!')
        number_of_vowels = sum(word.count(i) for i in vowels)
        print('Your word was : ',word,'Your Vowel count was: ',number_of_vowels)
        wordCount = wordCount + 1
        choice = input('Do you have another word? Y/N: ').lower().strip()

        if choice == 'n':
            averageV = int(number_of_vowels // wordCount)
            print('Your average number of Vowels was: ',averageV)
            print('Thank you for using Word Madness!')
            complete = True

        else:
            mode = 'vowels'

#If consonants are selected then they will be counted

    elif mode == 'consonants':
        word = input('Please enter your word!')
        number_of_consonants = sum(word.count(i) for i in consonants)
        print('Your word was : ',word,'Your Consonant count was: ',number_of_consonants)
        wordCount = wordCount + 1
        choice = input('Do you have another word? Y/N: ').lower().strip()

        if choice =='n':
            averageC = int(number_of_consonants // wordCount)
            print('Your average number of Consonants was: ',averageC)
            print('Thank you for using Word Madness!')
            complete = True
#If user has no more words to enter then they are given an average

         else:
            mode == 'consonants'

    else:
        print('ERROR! INVALID INPUT DETECTED!')

2 个答案:

答案 0 :(得分:0)

好的,根据我的理解,你不知道怎么回到代码中。为此,您应该学习如何在Python中使用functions。 什么是功能?

函数是一个有组织的,可重用的代码块,用于执行单个相关操作。函数为您的应用程序提供了更好的模块化,并提供了高度的代码重用。 (取自互联网的定义)

所以我建议你找一些关于功能的更多信息,因为它非常有用。 学习功能后,你应该添加:

每次

之后
 if choice =='n':
        averageC = int(number_of_consonants // wordCount)
        print('Your average number of Consonants was: ',averageC)
        print('Thank you for using Word Madness!')
        complete = True

添加

elif choice == 'n':
        function() 

功能() - >调用主要功能。

答案 1 :(得分:0)

从您的问题和评论中,我认为您想要问

  

mode = input('你想键入元音,辅音或退出吗?:')。lower()。strip()

只有一次。如果是这种情况,您可以将该语句移到while循环上方。 或者,您可以选择用户是否真的想再次指定模式。