Skip to main content

Pilot · Lab P01 · 45 minutes

First Python input and output

Write a short Python program that asks for a club name and the number of places left, then prints one clear sentence. It must reject input that is not a whole number of 0 or more instead of crashing or printing nonsense.

Predict first

Write your predictions before you use the tool.

  1. For the inputs Art and 5, write the exact sentence you expect the example program to print.
  2. For Code and 0, what should a useful program say? Is “Code has 0 places left.” wrong, or just unhelpful?
  3. What should happen if someone types -1 or five for places? Write your expected output before using the tracer.

Practice tool

The tracer below follows one fixed example program line by line for the inputs you type. It is a step-by-step simulation of that exact program, written for this lab. It does not run Python and it cannot run your code. Your own program is saved only if your teacher assigns this lab, and your teacher reviews it.

Input/output tracer · fixed example program

The tracer keeps the first 30 characters.

Try 5, 0, -1, five, 007, a leading space, or ².

Trace the program to step through these inputs. Changing an input clears the previous trace.

Starter

Starter (incomplete): club.py · Python
name = input("Club name: ")places = input("Places left: ")# TODO 1: check that places is only the digits 0-9 (places.isascii() and places.isdigit())# TODO 2: if it is 0, say the club is full# TODO 3: otherwise print "<name> has <places> places left."print(name + " has " + places + " places left.")
Example program used by the tracer · Python
name = input("Club name: ")text = input("Places left: ")if not (text.isascii() and text.isdigit()):    print("Places must be a whole number 0 or more.")elif int(text) == 0:    print(name + " is full.")else:    print(name + " has " + text + " places left.")

Worked case

input() always returns text. Even when someone types 5, the variable holds the string "5". That is why the example checks text.isascii() and text.isdigit() before treating it as a number. Together they accept only the plain digits 0–9, so "-1" fails (the minus sign is not a digit), "" fails (an empty string has no digits) and " 5" fails (a space is not a digit).

Worked trace for name = Art, places = 5
LineWhat happensVariables after
1input returns "Art"name = "Art"
2input returns "5" (text, not a number)name = "Art", text = "5"
3"5".isascii() and "5".isdigit() are both True, so not (…) is Falseunchanged
5int("5") == 0 is Falseunchanged
8prints Art has 5 places left.unchanged

Build

  1. Start from the starter. Complete TODO 1–3 so your program matches the specification.
  2. Choose one clear wording for each message and keep it consistent.
  3. Use the tracer to compare your predictions with the example program. Then check your own program by reading it line by line against the test table: the tracer cannot run it.
  4. If your school provides an approved Python environment, you may also run your program there and record what it printed. Never paste output you did not observe.

Tests

Tests for this lab
InputExpectedWhy this test
Art, 5Art has 5 places left.Normal case.
Code, 0Code is full.Boundary: zero is valid but deserves a different message.
Art, -1Places must be a whole number 0 or more.Negative numbers are rejected.
Art, fivePlaces must be a whole number 0 or more.Non-numeric text is rejected rather than crashing.
Art, (empty)Places must be a whole number 0 or more.Pressing Enter with nothing typed gives "", which has no digits.
Art, " 5"Places must be a whole number 0 or more.Whitespace is not a digit. This lab does not strip spaces.
Art, 007Art has 007 places left.Leading zeros are valid digits. int("007") is 7, but the message prints the original text.
Art, ²Places must be a whole number 0 or more.isdigit() alone accepts "²", but int("²") fails. isascii() rejects it first.

Explain and reflect

Explain

  1. Explain why input("Places left: ") gives text even when the user types digits, and how your program deals with that.
  2. Explain the difference between this tracer and running Python: what does each one tell you?

Reflect

  1. Which test caught a mistake in your first version, or which would have?
  2. Name one other input your validation might not handle (for example, spaces around the number) and what you would do about it.

What this lab does not do

  • The tracer follows only the fixed example program; it never runs your code.
  • Python arbitrary-code execution is not available on this site. Your teacher reviews your code.
  • Nothing you type into the tracer is saved.
  • Valid places means only the ASCII digits 0–9, by design. The tracer keeps the first 30 characters of each input.