Kaggle — Learn Python Challenge: Day 5

Editor: Ishmael Njie

DataRegressed Team
DataRegressed
3 min readJun 16, 2018

--

Day 5 is here!

Here are the links to the previous exercises:

Day 1 — Hello Python!(Variable assignment etc.)

Day 2 — Functions and Getting Help

Day 3 — Booleans and Conditionals

Day 4 — Lists

Day 5 was all about loops. My notebook for these set of exercises can be found here!

  • Exercise 1:

The first exercise was around debugging a function; the function is supposed to return True if there is a number in the list argument that is divisible by 7:

If we call this function:

The output here is False. However, we can clearly see that there is a number in the list that is divisible by 7.

The function will work if we remove the ‘else’ keyword and indent the last return correctly:

The ‘return’ keyword causes a function to exit immediately. So previously, the function only ran for one iteration as it would have identified 1 to not be divisible by 7.

  • Exercise 2:

Part a:

A TypeError was thrown with this particular line of code, as the > operator cannot compare instances of different types: lists and integers.

Part b:

Based on the code above, this part was about defining a function that returned True or False when comparing each element of the list to 2. True is the number is greater than 2 and False otherwise.

Here, we append a new list, called ‘greater’, with the boolean output of the comparisons on each element with 2.

  • Exercise 3:

This exercise consisted of defining a function that returned True if the same meal was served two days in a row, and False otherwise.

  • Exercise 4:

Exercise 4 was all about a slot machine. A function ‘play_slot_machine()’ is already defined, and outputs a number relating to winnings in dollars. It often returns 0, but with these slot machines, you could get lucky and get a number out.

As it costs 1$ to play the machine, we are to define a function that returns the average payout per play.

  • Exercise 5:

As it is 1$ to play the slot machine, how many spins can you play before running out of money?

If you have x$, you can definitely have x spins, but you may get a payout which will allow for more spins!

This task was centred around completing the function to estimate the probability of completing a given number of spins before running out of money.

For each simulation, we have a start balance and a number of spins. While we have x$ and an n number of spins left, update our balance each time we play the slot machine. Every time we meet the number of spins (spins = 0), we have succeeded. The returned value is an average given a number of simulations.

Follow the notebook for these exercises here and stay tuned for Day 6!

--

--