Calculate Average Ratings for Restaurants with Python

School
Rensselaer Polytechnic Institute**We aren't endorsed by this school
Course
CSCI 1100
Subject
Marketing
Date
Dec 10, 2024
Pages
1
Uploaded by AmbassadorElkMaster1201
import lab05_utildef print_info(restaurant):name = restaurant[0]restaurant_type = restaurant[5]address = restaurant[3].split('+')scores = restaurant[6]num_reviews = len(scores)if num_reviews < 3:average_score = sum(scores) / num_reviewselse:total = sum(scores)average_score = (total - max(scores) - min(scores)) / (num_reviews - 2)if average_score < 2:rating = "This restaurant is rated bad, based on {} reviews.".format(num_reviews)elif average_score < 3:rating = "This restaurant is rated average, based on {} reviews.".format(num_reviews)elif average_score < 4:rating = "This restaurant is rated above average, based on {} reviews.".format(num_reviews)else:rating = "This restaurant is rated very good, based on {} reviews.".format(num_reviews)print("{} ({})".format(name, restaurant_type))for line in address:print("\t{}".format(line.strip())) print(rating)restaurants = lab05_util.read_yelp('yelp.txt')try:restaurant_id = int(input("Enter a restaurant ID (1 to {}): ".format(len(restaurants))))if 1 <= restaurant_id <= len(restaurants):print_info(restaurants[restaurant_id - 1]) else:print("Warning: Invalid restaurant ID. Please enter a number between 1 and", len(restaurants))except ValueError:print("Warning: Please enter a valid integer.")
Background image