livepython.net

202. Python Program to Count Positive, Negative and Zero Numbers in a List

EasyPhase 1 · Warm-up

This one uses split(), a for loop and if, elif and else. You know them all.

Overview

Putting numbers into groups and counting each group is a common interview warm-up. It is the same idea that reports and charts use.

Problem Statement

Noah writes down the temperature every morning, in whole degrees. Some mornings are above 0, some are below 0, and some are exactly 0.

Read the temperatures. Count how many are positive (more than 0), how many are negative (less than 0) and how many are zero.

Steps

  1. Read the line and split it into a list.
  2. Make three counters that start at 0.
  3. Loop over the list. Turn each piece into a number, and add 1 to the right counter.
  4. Print the three counts, one per line, in the order shown below.

Examples

Example 1. Input 3 -1 0 7 -5 gives:

Positive: 2
Negative: 2
Zero: 1

Why: 3 and 7 are more than 0, -1 and -5 are less than 0, and there is one 0.

Example 2. Input 0 0 0 gives:

Positive: 0
Negative: 0
Zero: 3

Why: 0 is neither positive nor negative, so all three numbers count as zero.

Input

One line of whole numbers, with one space between them (1 number or more). Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.

Output

Three lines, in this order:

Positive: <count>
Negative: <count>
Zero: <count>

The tests try 5 different inputs.

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

1Positive: 2
2Negative: 2
3Zero: 1

YOUR OUTPUT

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