Supposing movies
is a normal Python List and that you would like to get the average cost in Dollars:
movies = [
("Titanic", 20000000),
("Dracula", 9000000),
("James Bond", 4500000),
("Pirates of the Caribbean: On Stranger Tides", 379000000),
("Avengers: Age of Ultron", 365000000),
("Avengers: Endgame", 356000000),
("Incredibles 2", 200000000)
]
totalCost = 0
totalMovies = 0
for movie, price in movies:
totalCost += price
totalMovies +=1
print (f"The average cost per movie is {totalCost/totalMovies:.2f}$")
The line {totalCost/totalMovies:.2f}
is inserting the computation in-line making use of the Python F strings
while selecting two decimal values (.2f
)
Hope everything was clear!
solved Calculate the average budget of all movies in the data set [closed]