Python Level 1 -- Wednesdays @ 2:40pm


Contents

Lesson 1 | Sep. 16

Topics

Code

Homework

Part 1

Modify hello.py to print a sentence of your choice.

Now try printing more than one sentence.

Now try printing more than one sentence, each on its own line, using only one print statement. (HINT: use "\n" to tell Python to make a new line)

Part 2

Make Python prompt (ask) you for a sentence as input, and then read that sentence back to you as output.

Part 3

Explore what other possibilities there are with input() and print().

You can create a text-based game, a personality quiz, a madlibs game...

Just try stuff out and see what you can create!

Lesson 2 | Sep. 23

Topics

Code

Homework

Remember: these are optional, but highly encouraged. To that end, I made this week's assignments much more difficult than last week. I encourage you to try them all and it's totally okay if you struggle! If you are really stuck and you don't know what to do, it's also totally okay to ask for help by looking it up on the internet or asking someone you know to help you. Programming is a collaborative process; you aren't expected to do everything perfectly and you don't need to memorize anything!

I. Casting

Store various data types in different variables and try converting between them.

Record what happens. Do any conversions cause errors? Look for strange edge cases.

What happens when you convert a float to an int?

II. Cashier

Write a program called cashier.py that does the following:

  1. Ask user for an amount of money (the change owed) as input. This should be in the form "3.92" to represent 3 dollars and 92 cents. Hint: what data type is this? Every data type has a casting function like int(). For example, to cast to a string, you use str().
  2. Convert this amount to pennies by multiplying by 100.
  3. Starting with dollars, then quarters, and so on to pennies, compute the fewest number of coins necessary to fill the given amount. For example, 3.92 cents is most efficiently broken up into 3 dollars, 3 quarters, 1 dime, 1 nickel, and 2 pennies.
  4. Print out how many of each coin is required to give the user their change.

You will have to use a variable for each of the coins to store how many of them are needed.

To make this work, you will need one more operator we didn't learn about in class: the modulus operator, written in Python as %. This is actually simpler than it seems; it just returns the remainder of the division of two numbers. For example, 9 % 2 = 1 and 10 % 5 = 0 and 92 % 25 = 17.

Good luck!

Solution

III. Pseudcode

Pseudocode is a way of writing or designing programs without actually having to bother with the nitty-gritty of Python programming. Pseudocode will not work in Python, but it is helpful as a first step to plan out your program.

It's also useful for intuitively understanding more advanced code structures, which we will learn about next week!

Here's an example expanding on one of the programs we did in class, in Python and then in pseudocode:

Python:
num1 = int(input("Enter a number: "))
num2 = int(input("Enter another number: "))
print(num1, "^", num2, "=", num1**num2)
Pseudocode:
1. Get an integer from the user and store it in the variable 'num1'.
2. Get an integer from the user and store it in the variable 'num2'.
3. Raise num1 to the power of num2 and print the result.

As you can see, the pseudocode was a lot more readable. Unfortunately, it's also not valid code, so you'd need to translate it to Python to make it work. Still, it's great as a way to write out the important bits of your program and think about what it's doing without having to worry about types and conversions and all those low-level details during the planning phase.

There's no official way to write pseudocode. Some people prefer like above, others prefer something more code-looking:

num1 = get_int()
num2 = get_int()
result = pow(num1, num2)
print(result)

This looks a bit more like valid Python, but it's still not. get_int() is not a defined function in Python (though you could write it yourself). Here, it is used as an abstraction -- who cares how get_int() actually works? I just want to get an integer! It covers up the details and focuses on the important part, which is that we need to get an int from the user.

The following exercises should be written entirely in pseudocode. That is, you don't even need REPL for this! You can write them on paper or in a text document like Word.
Exercise 1

Write, in pseudocode, instructions for how to make a peanut butter and jelly sandwich. Be as specific as possible -- remember, you're giving instructions to a robot or a computer! Computers take you literally and they'll get stuck if you aren't specific enough.

For example, it doesn't suffice to say "scoop out some jelly." Where are you scooping out from? What are you scooping out with? How much is "some"?

When you're finished, watch this video.

Exercise 2

Write a program in pseudocode explaining how to count from 1 to 100. Just like in Exercise 1, it's important to be specific!

Exercise 3

Write a program in which the computer picks a random number from 1-10 and allows the user 4 guesses to figure out what it is. If the user guesses correctly, say "Congratulations" and stop the program. If they run out of guesses, say "You lost!", tell the user what the secret number was, and stop the program.

Lesson 3 | Sep. 30

Topics

Code

Homework

I. Number guessing game: user guessing

Solution. I encourage you to try it yourself first!

Use if and while loops to create a game that:

  1. Generates a random number from 1 to 10
  2. Lets the user attempt to guess the number (4 guesses allowed)
  3. If the user runs out of guesses before they guess correctly, tell them what the secret number was and end the program.

It helps to write out the entire program in pseudocode first and then translate that to actual Python. Try to be as specific as possible!

This program requires a few things we didn't learn in class. Here they are:

To generate a random number from 1 to 10, put the following code at the top of your program:

import random
secret = random.randint(1,10)

In class we only briefly saw using if/else blocks as "if this, then that, else that". But now we have three options. To write more complicated conditional blocks, we can either nest them or use a third keyword: elif (which just means "else" followed by another "if"). It looks like this:

if temperature > 90:
    print("It's super hot outside!")
elif temperature > 68:
    print("It's warm outside.")
elif temperature > 32:
    print("It's cold outside.")
else:
    print("It's freezing outside!")

As you can see, you can put together long series of if/elif/else blocks this way. This will help break down the three possibilities: you guessed too high, too low, or just right.

Remember—looking stuff up is NOT against the rules! I encourage you to use Google to solve problems if you get stuck.

II. Number guessing game: computer guessing

Solution. I encourage you to try it yourself first!

Write the same program as above, but swap the roles. The user will think of a number and the computer has 4 attempts to guess the number. The user will type "h" if the guess was too high, "l" if the guess was too low, and "c" if the guess was correct.

You can compare strings just like you compare numbers: e.g., feedback == "h" can evaluate to True or False depending on the value of the variable feedback. This is case-sensitive, meaning "H" is not equal to "h".

For this to work, you will need to think carefully about how to give the computer some "intelligence" about what to guess next. You could, admittedly, reuse the code from Part I that just guesses a random number and then hope one of the four guesses is correct, but it would be more effective to take advantage of the user's feedback. If the user says the computer guessed too high, then maybe the computer should start guessing lower.

How do you guess lower? Consider keeping two variables that track the range of possibilities of the number. Start from 1 to 10, and maybe guess halfway between: 5. If this was too high, you know you can ignore numbers 5-10, so the new "high" becomes 4. So the computer guesses halfway between 1 and 4: 2. And so on until correct or you ran out of guesses.

Lesson 4 | Oct. 7

Apologies for a rather hectic class! I have written up some detailed notes to help get you acquainted with lists, as they are a super important topic! Access them below:

Topics

Detailed notes on lists

Code

Homework

No homework this week! Just practice using lists.

Lesson 5 | Oct. 14

Topics

Code

Homework

I've become aware that homework is a great evil in this world, so again, no homework this week.

That being said, I encourage you to look at this week's code and try writing it yourself without looking, so you really understand it.

This week is one of the most important in terms of practical programming skills and problem-solving. An unreal amount of problems can be reduced to some form of searching a list, so knowing how to do that properly is super useful.

Lesson 6 | Oct. 21

Topics

Code

Lesson 7 | Oct. 28

Topics

Code

Lesson 8 | Nov. 11

Topics

Code

Lesson 9 | Nov. 18

Topics

Code