Please help me fix my errors in Python. def read_data(filename): try: with open(filename, "r") as file: return file.read() except Exception as exception: print(exception) return None def extract_data(tags, strings): data = [] start_tag = f"<{tags}>" end_tag = f"" while start_tag in strings: try: start_index = strings.find(start_tag) + len(start_tag) end_index = strings.find(end_tag) value = strings[start_index:end_index] if value: data.append(value) strings = strings[end_index + len(end_tag):] except Exception as exception: print(exception) return data def get_names(string): names = extract_data("name", string) return names def get_descriptions(string): descriptions = extract_data("description", string) return descriptions def get_calories(string): calories = extract_data("calories", string) return calories def get_prices(string): prices = extract_data("price", string) return prices def display_menu(names, descriptions, calories, prices): for i in range(len(names)): print(f"{names[i]} - {descriptions[i]} - {calories[i]} - {prices[i]}") def display_stats(names, calories, prices): try: total_items = len(names) average_calories = (sum(map(int, calories)) / total_items if total_items > 0 else 0) average_price = (sum([float(price[1:]) for price in prices]) / total_items if total_items > 0 else 0) if names and calories and prices: print(f"\n{total_items} items - {average_calories:.1f} average calories - ${average_price:.2f} average price") except: print("Please insert a valid number.") def main(): filename = "simple.xml" string = read_data(filename) if string: names = get_names(string) descriptions = get_descriptions(string) calories = get_calories(string) prices = get_prices(string) if names and prices and descriptions and calories: display_menu(names, descriptions, calories, prices) display_stats(names, calories, prices) main()
Please help me fix my errors in Python.
def read_data(filename):
try:
with open(filename, "r") as file:
return file.read()
except Exception as exception:
print(exception)
return None
def extract_data(tags, strings):
data = []
start_tag = f"<{tags}>"
end_tag = f"</{tags}>"
while start_tag in strings:
try:
start_index = strings.find(start_tag) + len(start_tag)
end_index = strings.find(end_tag)
value = strings[start_index:end_index]
if value:
data.append(value)
strings = strings[end_index + len(end_tag):]
except Exception as exception:
print(exception)
return data
def get_names(string):
names = extract_data("name", string)
return names
def get_descriptions(string):
descriptions = extract_data("description", string)
return descriptions
def get_calories(string):
calories = extract_data("calories", string)
return calories
def get_prices(string):
prices = extract_data("price", string)
return prices
def display_menu(names, descriptions, calories, prices):
for i in range(len(names)):
print(f"{names[i]} - {descriptions[i]} - {calories[i]} - {prices[i]}")
def display_stats(names, calories, prices):
try:
total_items = len(names)
average_calories = (sum(map(int, calories)) / total_items if
total_items > 0 else 0)
average_price = (sum([float(price[1:]) for price in prices]) /
total_items if total_items > 0 else 0)
if names and calories and prices:
print(f"\n{total_items} items - {average_calories:.1f} average calories - ${average_price:.2f} average price")
except:
print("Please insert a valid number.")
def main():
filename = "simple.xml"
string = read_data(filename)
if string:
names = get_names(string)
descriptions = get_descriptions(string)
calories = get_calories(string)
prices = get_prices(string)
if names and prices and descriptions and calories:
display_menu(names, descriptions, calories, prices)
display_stats(names, calories, prices)
main()
Step by step
Solved in 5 steps with 4 images