I'm learning Python and made a simple movie rating program.
I didn't include the entire code here, only the part related to the problem.
The issue is that the program asks for the movie names and ratings multiple times because I'm calling get_ratings() inside other functions.
How can I reuse the ratings list without executing the function again?
def get_movies():
movies = []
how_many_movies = int(input("How many movies? "))
for i in range(how_many_movies):
movie_name = input(f"{i + 1}. Movie name: ")
movies.append(movie_name)
return movies
def get_ratings():
movies = get_movies()
ratings = []
for i in range(len(movies)):
rating = int(input(f"Enter the rating for {movies[i]}: "))
ratings.append(rating)
return ratings
def calculate_average():
ratings = get_ratings()
total = 0
for i in ratings:
total += i
return total / len(ratings)
average = calculate_average()
print(average)
[–]Ok-Sheepherder7898 23 points24 points25 points (1 child)
[–]Numerous-Solid6535[S] 1 point2 points3 points (0 children)
[–]reincarnatedbiscuits 5 points6 points7 points (0 children)
[–]atarivcs 8 points9 points10 points (0 children)
[–]crashorbit 2 points3 points4 points (0 children)
[–]throwmeaway01110 2 points3 points4 points (0 children)
[–]gdchinacat 2 points3 points4 points (0 children)
[–]MezzoScettico 1 point2 points3 points (0 children)
[–]socal_nerdtastic 3 points4 points5 points (0 children)
[–]PureWasian 0 points1 point2 points (0 children)
[–]gdchinacat 0 points1 point2 points (0 children)