This notebook is an exercise in the Python course. You can reference the tutorial at this link.


Try It Yourself

Think you are ready to use Booleans and Conditionals? Try it yourself and find out.

To get started, run the setup code below before writing your own code (and if you leave this notebook and come back later, don't forget to run the setup code again).

Exercises

1.

Many programming languages have sign available as a built-in function. Python doesn't, but we can define our own!

In the cell below, define a function called sign which takes a numerical argument and returns -1 if it's negative, 1 if it's positive, and 0 if it's 0.

2.

We've decided to add "logging" to our to_smash function from the previous exercise.

What happens if we call it with total_candies = 1?

That isn't great grammar!

Modify the definition in the cell below to correct the grammar of our print statement. (If there's only one candy, we should use the singular "candy" instead of the plural "candies")

3. 🌶️

In the main lesson we talked about deciding whether we're prepared for the weather. I said that I'm safe from today's weather if...

The function below uses our first attempt at turning this logic into a Python expression. I claimed that there was a bug in that code. Can you find it?

To prove that prepared_for_weather is buggy, come up with a set of inputs where either:

To get credit for completing this question, your code should return a Correct result.

4.

The function is_negative below is implemented correctly - it returns True if the given number is negative and False otherwise.

However, it's more verbose than it needs to be. We can actually reduce the number of lines of code in this function by 75% while keeping the same behaviour.

See if you can come up with an equivalent body that uses just one line of code, and put it in the function concise_is_negative. (HINT: you don't even need Python's ternary syntax)

5.

The boolean variables ketchup, mustard and onion represent whether a customer wants a particular topping on their hot dog. We want to implement a number of boolean functions that correspond to some yes-or-no questions about the customer's order. For example:

For each of the remaining functions, fill in the body to match the English description in the docstring.

6. 🌶️

We’ve seen that calling bool() on an integer returns False if it’s equal to 0 and True otherwise. What happens if we call int() on a bool? Try it out in the notebook cell below.

Can you take advantage of this to write a succinct function that corresponds to the English sentence "does the customer want exactly one topping?"?

7. 🌶️ (Optional)

In this problem we'll be working with a simplified version of blackjack (aka twenty-one). In this version there is one player (who you'll control) and a dealer. Play proceeds as follows:

When calculating the sum of cards, Jack, Queen, and King count for 10. Aces can count as 1 or 11 (when referring to a player's "total" above, we mean the largest total that can be made without exceeding 21. So e.g. A+8 = 19, A+8+8 = 17)

For this problem, you'll write a function representing the player's decision-making strategy in this game. We've provided a very unintelligent implementation below:

This very conservative agent always sticks with the hand of two cards that they're dealt.

We'll be simulating games between your player agent and our own dealer agent by calling your function.

Try running the function below to see an example of a simulated game:

The real test of your agent's mettle is their average win rate over many games. Try calling the function below to simulate 50000 games of blackjack (it may take a couple seconds):

Our dumb agent that completely ignores the game state still manages to win shockingly often!

Try adding some more smarts to the should_hit function and see how it affects the results.

Keep Going

Learn about lists and tuples to handle multiple items of data in a systematic way.


Have questions or comments? Visit the Learn Discussion forum to chat with other Learners.