1.1. Exercises
Quick Questions with Conditionals
time = 150
if time < 120:
speed = 'Fast'
elif time < 180:
speed = 'Average'
else:
speed = 'Slow'
speedprice = 150
if price > 50:
expensive = 'moderately'
elif price > 100:
expensive = 'valuable'
else:
expensive = 'affordable'
expensiveWill it Run with Conditionals
if price > 100
expensive = 'valuable'
else
expensive = 'affordable'
expensiveif price > 50:
expensive = 'moderately'
elif price > 100:
expensive = 'valuable'
else:
expensive = 'affordable'
expensiveif price > 50:
expensive = 'moderately'
expensiveCoding questions
Instructions:
Running a coding exercise for the first time could take a bit of time for everything to load. Be patient, it could take a few minutes.
When you see ____ in a coding exercise, replace it with what you assume to be the correct code. Run it and see if you obtain the desired output. Submit your code to validate if you were correct.
Make sure you remove the hash (#) symbol in the coding portions of this question. We have commented them so that the line won’t execute and you can test your code after each step.
Creating Conditional Statements
Let’s practice making decisions with conditional statements. We are going to the gym and our exercise plan takes different amounts of reps. let’s make conditional statements that depend on the name of the exercises.
Tasks:
- Make
if,elif, andelsestatements for the following conditions:- if the exercise value is
lunges, set an object value namedrepsto 20. - if the exercise value is
squats, setrepsto 25 - if the exercise value is
burpees, setrepsto 15 - If the exercise value is anything else, set
repsto 10.
- if the exercise value is
- Display the value of
reps.
Creating an Inline if/else Statement
Let’s try to make inline conditional statements using if and else. the variable cups_of_tea is the number of cups of tea Ben drank last week.
Tasks:
- Make an inline if/else statement that satisfies the following conditions:
- if the data type of
cups_of_teais of typelist, then set and object namedtea_amountto the sum of the elements. - if the data type is anything else, set
tea_amountto the string'cannot sum'.
- if the data type of
- Display the result of
tea_amount.