Essays
Topics
Writing Tool
Machine Learning AI
ChatGPT
US History
Presidents of the United States
Joseph Robinette Biden
Donald Trump
Barack Obama
US States
States Ranked by Size & Population
States Ranked by Date
IPL
>
Marketing
>
Calculate Average Ratings for Restaurants with Python
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_util
def 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_reviews
else:
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.")