Determine your next move
In the Empire State Building bet, your next move depends on the number of eyes you throw with the dice. We can perfectly code this with an if-elif-else construct!
The sample code assumes that you're currently at step 50. Can you fill in the missing pieces to finish the script?
This exercise is part of the course
Intermediate Python for Data Science
Exercise instructions
- Roll the dice. Use
randint()to create the variabledice. - Finish the
if-elif-elseconstruct by replacing___:- If
diceis 1 or 2, you go one step down. - if
diceis 3, 4 or 5, you go one step up. - Else, you throw the dice again. The number of eyes is the number of steps you go up.
- If
- Print out
diceandstep. Given the value ofdice, wasstepupdated correctly?
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import numpy and set seed
import numpy as np
np.random.seed(123)
# Starting step
step = 50
# Roll the dice
# Finish the control construct
if dice <= 2 :
step = step - 1
elif ___ :
___
___ :
step = step + np.random.randint(1,7)
# Print out dice and step