livepython.net

201. Python Program to Count How Many Times a Number Appears in a List

EasyPhase 1 · Warm-up

You know split(), for loops and counting with if. That is all you need here.

Overview

Counting how often a value appears is a common interview warm-up. Shops, games and surveys count things like this all the time.

Problem Statement

Mei runs a quiz night. The scores of all the players are on one line. Mei wants to know how many players got a certain score.

Read the scores on the first line and the score to find on the second line. Print Count of <number>: <times>.

Steps

  1. Read the first line and split it into a list.
  2. Read the second line and turn it into a whole number.
  3. Start a counter at 0. Loop over the list, and add 1 each time a number equals the one you look for.
  4. Print the number you looked for and the count.

Examples

Example 1. Input:

3 5 3 8 3
3

gives:

Count of 3: 3

Why: the number 3 is in the list three times: first, third and last.

Example 2. Input:

1 2 3
7

gives:

Count of 7: 0

Why: 7 is not in the list at all, so the count stays 0.

Input

Two lines. Line 1: whole numbers with one space between them (1 number or more). Line 2: the whole number to find. Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.

Output

One line: Count of <number>: <times>. The tests try 6 different inputs, also negative numbers and 0.

Try it first. The solution opens when you solve this question, or after 3 tries with Submit.

Finish this chapter firstChapter 12 · Working With Strings
solution.py
# write your code here

EXPECTED OUTPUT

1Count of 3: 3

YOUR OUTPUT

Press Run to see your output here. Lines are compared with the expected output.