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()

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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()

 

 

Run CIS 106 Python Tests
failed 1 minute ago in 12s
Test with pytest
581
582
583
584
585
586
587
588
589
590
591
JUJ vulpul.
566
FAILED .github/workflows/test_final_project.py::test_final_project_no_records - AssertionError: Final Project Final Project Error message is missing or incorrect. Expected
'File is empty' or 'No data'.
567 Input:
568 No records
569 Output:
570 FAILED .github/workflows/test_final_project.py::test_final_project_missing_fields
Expected 'Error: Missing or bad data'.
571 Input:
572 Missing fields
573 Output:
$5.95
574 Belgian Waffles Two of our famous Belgian Waffles with plenty of real maple syrup 650
575 Strawberry Belgian Waffles Light Belgian waffles covered with strawberries and whipped cream 900 - $7.95
576 Berry-Berry Belgian Waffles - Light Belgian waffles covered with an assortment of fresh berries and whipped cream 900 - $8.95
577
Homestyle Breakfast - Thick slices made from our homemade sourdough bread 600 - $4.50
578
579 4 items 1000.0 average calories $8.58 average price
580
Output:
Belgian Waffles
FAILED .github/workflows/test_final_project.py::test_final_project_bad_data - AssertionError: Final Project Final Project Error message is missing or incorrect. Expected
'Error: Missing or bad data'.
Input:
Bad data
Two of our famous Belgian Waffles with plenty of real maple syrup 650 $5.95
Strawberry Belgian Waffles - Light Belgian waffles covered with strawberries and whipped cream
900 $7.95
Berry-Berry Belgian Waffles Light Belgian waffles covered with an assortment of fresh berries and whipped cream 900 $8.95
French Toast Thick slices made from our homemade sourdough bread
600 - X
Homestyle Breakfast Two eggs, bacon or sausage, toast, and our ever-popular hash browns
Please insert a valid number.
6 failed, 45 passed, 35 skipped in 1.62s
Error: Process completed with exit code 1.
Post Set up Python
Post Copy repository
-
Complete job
Q Search logs
AssertionError: Final Project Final Project Error message is missing or incorrect.
65
-
950 $6.95
2s
Øs
1s
Øs
Transcribed Image Text:Run CIS 106 Python Tests failed 1 minute ago in 12s Test with pytest 581 582 583 584 585 586 587 588 589 590 591 JUJ vulpul. 566 FAILED .github/workflows/test_final_project.py::test_final_project_no_records - AssertionError: Final Project Final Project Error message is missing or incorrect. Expected 'File is empty' or 'No data'. 567 Input: 568 No records 569 Output: 570 FAILED .github/workflows/test_final_project.py::test_final_project_missing_fields Expected 'Error: Missing or bad data'. 571 Input: 572 Missing fields 573 Output: $5.95 574 Belgian Waffles Two of our famous Belgian Waffles with plenty of real maple syrup 650 575 Strawberry Belgian Waffles Light Belgian waffles covered with strawberries and whipped cream 900 - $7.95 576 Berry-Berry Belgian Waffles - Light Belgian waffles covered with an assortment of fresh berries and whipped cream 900 - $8.95 577 Homestyle Breakfast - Thick slices made from our homemade sourdough bread 600 - $4.50 578 579 4 items 1000.0 average calories $8.58 average price 580 Output: Belgian Waffles FAILED .github/workflows/test_final_project.py::test_final_project_bad_data - AssertionError: Final Project Final Project Error message is missing or incorrect. Expected 'Error: Missing or bad data'. Input: Bad data Two of our famous Belgian Waffles with plenty of real maple syrup 650 $5.95 Strawberry Belgian Waffles - Light Belgian waffles covered with strawberries and whipped cream 900 $7.95 Berry-Berry Belgian Waffles Light Belgian waffles covered with an assortment of fresh berries and whipped cream 900 $8.95 French Toast Thick slices made from our homemade sourdough bread 600 - X Homestyle Breakfast Two eggs, bacon or sausage, toast, and our ever-popular hash browns Please insert a valid number. 6 failed, 45 passed, 35 skipped in 1.62s Error: Process completed with exit code 1. Post Set up Python Post Copy repository - Complete job Q Search logs AssertionError: Final Project Final Project Error message is missing or incorrect. 65 - 950 $6.95 2s Øs 1s Øs
Expert Solution
steps

Step by step

Solved in 5 steps with 4 images

Blurred answer
Knowledge Booster
Stack operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education