Now we’ll write that program we mentioned earlier. In the upper box, 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)
After you get that working, change your program into 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. So write a program that gives this output:
>>> %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 number: 50 The computer's number: 64
So the computer’s number is always 64. We don’t know how to pick a number randomly yet, so we’ll just do this instead.
computer_number = 64 guess = input("Pick a number: ") print("Your number:", guess) print("The computer's number:", computer_number)
Pretend you’re taking CS 101 at Wossamotta University. During the semester there are two midterm exams and a final exam. Your grade breakdown is:
You got 85% and 95% on the two midterms, respectively. So if you got 75% on the final, your grade would be 0.25 * 85 + 0.25 * 95 + 0.5 * 75 = 82.5%.
Now write the following program:
Enter a final exam score: 87 If you get 87 percent on the final exam, your grade will be 88.5 percent.
final = int(input("Enter a final exam score: ")) midterm1 = 85 midterm2 = 95 grade = 0.25 * midterm1 + 0.25 * midterm2 + 0.5 * final print("If you get", final, "percent on the final exam, your grade will be", grade, "percent.")
After you finish that, make a program that adds two numbers together:
Enter a number: 665 Enter another number: 1 665 plus 1 is 666