Python Arena
Step 4 of 4

Several inputs

A program can call input as many times as it needs. Each call reads the next line the user types, and each answer usually goes into a variable of its own:

dish = input("Dish: ")
drink = input("Drink: ")
print("Your order: " + dish + " with " + drink)
Input
pizza
lemonade
Output
Dish: pizza
Drink: lemonade
Your order: pizza with lemonade

The answers come in the order the program asks for them. A test on this site types one line for each input, in that same order.

A new value replaces the old one

When you store a new value in a variable that already has one, the old value is gone for good. That's fine when you're done with it:

guess = input("First guess: ")
print("You said " + guess)
guess = input("Second guess: ")
print("Now you say " + guess)
Input
blue
red
Output
First guess: blue
You said blue
Second guess: red
Now you say red

It's a bug when you still need the first value. Here both answers go into place, so the first one is lost before the program prints it:

place = input("Where were you born? ")
place = input("Where do you live now? ")
print("Born in " + place)
print("Living in " + place)
Input
Oslo
Rome
Output
Where were you born? Oslo
Where do you live now? Rome
Born in Rome
Living in Rome

The fix is a variable for each value you need to keep, such as birthplace and home.

Printing several values with commas

print can take several values separated by commas. It prints them all on one line, with a space between each two:

first = input("First word: ")
second = input("Second word: ")
print(first, second)
print("You typed", first, "and", second)
Input
sun
flower
Output
First word: sun
Second word: flower
sun flower
You typed sun and flower

So commas add the spaces for you, and + adds nothing. print("Hi", name) and print("Hi " + name) print the same line. When something must come right after a value, with no space, use +: print(name, "!") prints Ada !, but print(name + "!") prints Ada!.

Your task

Challenge 1 of 3

What does this program print? Type each line it prints.

Challenge 2

This program should ask for the names of two players and announce the match, but the first name gets lost. Fix it, so that a run looks like this:

Player 1: Ada
Player 2: Linus
Ada vs Linus

Challenge 3

Write a program that writes a postcard. It asks for the sender's name, the friend's name and a place, with the prompts shown below, and prints the card. If the user types Ada, Ben and Lisbon, the run looks like this:

Your name: Ada
Friend's name: Ben
Place: Lisbon
Dear Ben,
greetings from Lisbon!
Ada