← Back to course
Code Crew G6+ / Week 02 / Crew Quest
6/6
Week 02 Β· Variables and input

Crew Quest

Crew Quest: Mission badge
// Programs that remember and ask
⏱ about 30 min

Saturday: Crew Quest: Mission badge

It is badge day at Mission Control. Every crew member gets a small printed badge with a call sign, a station number and a favorite planet.

Rocket wants a program that makes the badges. "It asks three questions and prints a neat box," he says. "Easy."

Raven tilts her head. "What do you notice about the questions?" she asks. "A call sign is made up. It is not a real name, so it stays private and fun."

Nova sketches a badge outline in the air. "Interesting," she says. "Which values are numbers, and which are text? Decide before you write the code."

Rocket grabs paper, draws the badge first, and labels every value with its type.

  1. A variable is a name for a value. The = sign stores a value.
  2. The four main types are int, float, str and bool. type() tells you which.
  3. input() always gives a string. Use int() or float() to get a number.
  4. f-strings put values into text with curly brackets.
  5. ValueError means a value could not be used. TypeError means the types do not mix.
# badge.py
call_sign = input("Call sign: ")
station = int(input("Station number: "))
planet = input("Favorite planet: ")

print("+" + "-" * 22 + "+")
print(f"  CREW: {call_sign}")
print(f"  STATION: {station}")
print(f"  NEXT STOP: {planet}")
print("+" + "-" * 22 + "+")
Call sign: Comet
Station number: 4
Favorite planet: Mars
+----------------------+
  CREW: Comet
  STATION: 4
  NEXT STOP: Mars
+----------------------+
β—‡ BADGE WORKSHOP
Type and run badge.py together with a grown-up. Each of you invents a call sign, like Comet or Pathfinder.
Make a badge for each person. Then change the border characters to make your own style.
Ask your grown-up: which values are text and which is a number? Check your answer with type() in the shell.
WEEK 2 CHECK
  • Read the question.
  • Tap your answer.
station = int(input("Station number: ")). The user types 4. What type is station?
How many characters long is the border line "+" + "-" * 22 + "+"?
MORE REVIEW: WEEK 2
  • Read the question.
  • Tap your answer.
x = 4, then x = x * 3. What is x?
What does int("8") + 2 give?
What does f"{7 / 2:.1f}" show?

Great work. Next week your programs start making decisions.

← Friday