2012-05-24 52 views
-4

例子:如何使用while循環來計算結果?

choice(['Aa', 'Bb', 'Cc') 

應打印:

Enter choice: Aa 
Enter choice: Cc 
Enter choice: Aa 
Enter choice: Aa 
Enter choice: Bb 
Enter choice: Cc 
Enter choice: Aa 
Enter choice: 

There are 4 choices for Aa. 

There are 2 choices for Cc. 

There are 1 choices for Bb. 

There is 1 choice for Unknown. 

注意,它不斷要求你的選擇是什麼,直到有一個空白。

至今:

畫質選擇(N):

while True: 

    vote = input('Enter choice: ') 
+1

這種看起來像功課,其中老師給了你用戶如何與它交互並要求你寫它。 –

+0

'from collections import Counter' –

+0

這不是一個需要的問題。 –

回答

1

這聽起來很像功課,所以這裏的一些僞代碼:

function choice gets an options list 

    counter is a dictionary 
    for each option in the options list 
     counter[option] is 0 
    counter[unknown] is 0 

    loop 
     ask for input 
     if input is blank 
      exit the loop 
     otherwise if input is in the option array 
      increment counter[input] 
     otherwise 
      increment counter[unknown] 

    for each option in the options list 
     "Option {option} had {counter[option]} votes" 
    "Option unknown had {counter[unknown]} votes"