2017-07-04 141 views
1

這是我第一次在這個網站上,並且是編程新手。如果他們說「y」,我需要用戶輸入另一個單詞。截至目前,程序將它們發送回,而聲明。任何意見,將不勝感激。回到我的if語句

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!') 
+0

代碼對我來說似乎很好。你能澄清你在尋找代碼嗎? – ifconfig

+0

這裏有問題嗎?沒有任何明確的問題陳述。 –

+0

我很抱歉。問題是,當用戶輸入「y」時,而不是返回到if模式== x,它將返回到未完成狀態。想知道是否有一種方法讓用戶的輸入返回到他們輸入元音或輔音時,而不是重新開始播放節目。 –

回答

0

從你的問題和評論,我認爲你要問

模式=輸入(「你想輸入元音,輔音,或退出?:」).lower()。帶()

只有一次。如果是這種情況,可以將該語句移到while循環之上。 或者,您還可以給出一個選項,以確定用戶是否真的想要再次指定模式。

+0

工作正常!謝謝! –

+0

@DarrellHooper如果這是您所期望的答案,您可以將它標記爲答案嗎? –

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

謝謝!將確保使我在學習中優先考慮! –