In the upper box of Thonny, write some commands to do the following:
inputto get the user’s name
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
input pages to refresh your memory.
name = input("What is your name? ") print("Go away", name)
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
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:
>>> %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
computer_number = 64 guess = input("Pick a number: ") print("Your guess:", guess) print("The computer's number:", computer_number)