如何使用while循环计算结果?

时间:2012-05-24 21:59:11

标签: python

示例:

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.

请注意,在有空白之前,它会一直询问您的选择。

到目前为止:

def choice(n):

while True:

    vote = input('Enter choice: ')

1 个答案:

答案 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"