exercises

basic io

In the upper box of Thonny, write some commands to do the following:

  1. use input to get the user’s name
  2. use print to display a friendly greeting

After you write the commands, running the program should give you output like this:

What is your name? kevin
Go away kevin

Good luck! I’ve provided a solution below, but try to write the program without looking at it first. You may need to go back to the print, variables or input pages to refresh your memory.

Show solution

name = input("What is your name? ")
print("Go away", name)


mad libs

Make a game of Mad Libs:

enter a name: steve
enter another name: winnifred
enter a noun: large hadron collider
enter another noun (plural): salad
enter yet another noun: stopwatch
enter a verb ending in -ing: frollicking

steve and winnifred ran up the large hadron collider
to fetch a pale of salad
steve fell down and broke his stopwatch
and winnifred came frollicking after

high-low part 1

We’ll use what we learned to write a small part of the high-low program. In Thonny, create a file called hilo.py. We’ll add code to this file as we progress through the course.

For now, we want to write the part of the program that asks the user to guess a number. We don’t know how to pick random numbers yet, so we’ll make it so the computer’s number is always 64.

Write a program that does the following things:

  1. Store the number 64 in a variable called computer_number
  2. Ask the user to pick a number
  3. Tell the user what number they picked
  4. Tell the user what the computer’s number is

For example:

>>> %Run hilo.py
Pick a number: 7
Your guess: 7
The computer's number: 64

If you run it again but type 50 instead of 7, it’ll give this output:

>>> %Run hilo.py
Pick a number: 50
Your guess: 50
The computer's number: 64

Show solution

computer_number = 64
guess = input("Pick a number: ")
print("Your guess:", guess)
print("The computer's number:", computer_number)


previous: input next: arithmetic