Statement of Completion#4564979b
Python Basics
medium
Reinforcing Basic Control Flow Skills
Resolution
Activities
Fill in the Code¶
Decide Play Actions Based on Weather Conditions
In [1]:
def weather_decision(temperature):
if temperature >= 35:
return "It's too hot, I won't play."
elif temperature >= 15: # Fill the condition
return "It's normal, I will play."
else:
return "It's cold, I will play with a jacket."
2. Determine the Suitable Movie Based on Age¶
In [3]:
def movie_night(age):
if age >= 7: # Fill the condition
return "You can watch a movie."
else:
return "You should watch a cartoon movie."
3. Filter Fruits with More Than Five Characters¶
In [5]:
def filter_fruits(fruits):
result = []
for fruit in fruits:
# Fill the condition below
if len(fruit) > 5:
result.append(fruit)
return result
4. Maximize Shopping with a Given Budget¶
In [7]:
def shopping_budget(prices, budget):
total_spent = 0
for price in prices:
total_spent += price
if total_spent > budget: # Check after adding the current price
return "Out of Budget"
return total_spent
Predict the Output¶
5. Ice Cream Decision¶
In [ ]:
6. Exam Grades¶
In [ ]:
7. Water Consumption¶
In [ ]:
8. Travel Distance¶
In [ ]:
Correct the Code¶
9. Discount Checker¶
In [9]:
def check_discount(bill_amount):
if bill_amount > 1000:
return "Congratulations! You get a discount."
else:
return "No discount for you."
10. Weekend Planner¶
In [11]:
def weekend_plan(weather):
if weather == "sunny":
return "Go for a hike."
elif weather == "rainy":
return "Watch movies at home."
elif weather == "snowy":
return "Build a snowman."
else:
return "Stay home."
11. Find the Longest Word¶
In [18]:
def longest_word(words):
longest = "";
for word in words:
if len(word) >= len(longest): # Logical error: wrong comparison
longest = word
return longest
Code Activities¶
12. Gym Subscription¶
In [21]:
def gym_subscription(n_visits):
if n_visits == 0:
return "Inactive"
elif n_visits >= 1 and n_visits <= 3:
return "Basic"
else:
return "Premium"
13. Study Hours Tracker¶
In [27]:
def study_hours_tracker(study_hours):
list_categories = []
for study_hour in study_hours:
if study_hour >= 6:
list_categories.append("Excellent")
elif study_hour >= 3 and study_hour <= 5:
list_categories.append("Good")
else:
list_categories.append("Needs Improvement")
return list_categories
14. Marathon Planneer¶
In [34]:
5 * 1.10
Out[34]:
5.5
In [62]:
def marathon_planner(initial_distance, target_distance):
total_distance = initial_distance
day_distance = initial_distance
days = 1
while total_distance < target_distance:
total_distance += day_distance * 1.10
day_distance = day_distance * 1.10
days += 1
return total_distance, days
In [63]:
marathon_planner(5, 30)
Out[63]:
(30.525500000000005, 5)
In [64]:
def marathon_planner(initial_distance, target_distance):
if initial_distance <= 0 or target_distance <= 0:
raise ValueError("Distances must be positive")
current_day_distance = initial_distance
total_distance = initial_distance
days = 1
while total_distance < target_distance:
current_day_distance += current_day_distance * 0.1 # Increase by 10%
total_distance += current_day_distance
days += 1
return total_distance, days
In [65]:
marathon_planner(5, 30)
Out[65]:
(30.525499999999997, 5)
15. Vacation Budget¶
In [66]:
def vacation_budget(expenses, budget):
total_expenses = 0
for expense in expenses:
if expense > 500:
continue
if total_expenses <= budget:
total_expenses += expense
return total_expenses
In [67]:
vacation_budget([23, 560, 300, 200], 400)
Out[67]:
523
In [ ]: