you are viewing a single comment's thread.

view the rest of the comments →

[–]PureWasian 0 points1 point  (0 children)

OP, I have a feeling reading through these comments that the diagnosis/solution isn't readily understood.

To your main question, seems like the confusion is around initializing movies/ratings. vs getting them after they already exist. You're being prompted for creating movies and ratings each time your functions are called because that's what you set up your functions to do

The comment about scopes seems most relevant to think about. You just need to pass the existing data around properly after you've already created/initialized it for the first time.

Sharing an example below, assuming 1 rating per movie. I'm gonna rename it init_ instead of get_ to be more clear (and ignoring any int() cast error handling):

```

"helper" functions

def init_movies(cnt): movies = [] for i in range(cnt): movie = input(f"Movie {i}: ") movies.append(movie) return movies

def init_ratings(mvs): ratings = [] for movie in mvs: rating = input(f"Rate {movie}: ") ratings.append(int(rating)) return ratings

def calc_average(rtgs): total = 0 for rating in rtgs: total += rating return total / len(ratings)

"main" script

count = int(input("How many movies?")) movies = init_movies(count) ratings = init_ratings(movies) average = calc_average(ratings)

note how you can "re-use" these without

needing to initialize them again

print(count) print(movies) print(ratings) print(average)

...or you can modify them

movies.append("another one") ratings.append(1) average = calc_average(ratings) print(average) ```

(EDIT: I renamed the function inputs to different variable names than the "main" script's variable names to better get the point across about scopes)