Updating a variable with for loops
You're planning to hold a party so you can show off your playlist to friends and family. You've hired a venue that has a maximum capacity of 30 people, and you'd like to keep track of tickets sold.
This is a perfect opportunity to use a for loop - it will increment the value of a variable each time a ticket is sold and then print a statement once the event is sold out.
This exercise is part of the course
Introduction to Python for Developers
Exercise instructions
- Create a variable called
tickets_sold
with a value of0
. - Create a variable called
max_capacity
with a value of30
. - Loop through a range starting at
1
and finishing atmax_capacity
plus one, naming the iterator astickets
. - Inside the for loop, add
1
to the value oftickets_sold
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create the tickets_sold variable
____
# Create the max_capacity variable
____
# Loop through a range up to and including max_capacity's value
____ ____ ____ ____(____, ____):
# Add one to tickets_sold in each iteration
____ ____ ____
print("Sold out:", tickets_sold, "tickets sold!")