What's new

Help Sa mga marunong sa python filestream patingin po kung saan po ang mali badly needed huhu

The code you provided seems to have some issues. Let's go through them one by one:

1. In the write_dishes() function, you have a loop that asks for the number of dishes (num_dish) but you never update its value inside the loop. This means the loop will run indefinitely. To fix this, you can add an input statement inside the loop to update the value of num_dish.

2. Inside the loop in write_dishes(), you are writing the dish name to the file (outfile.write(dish + '\n')) but you are not writing the number of dishes. You can add a statement to write the number of dishes before entering the loop. For example: outfile.write(str(num_dish) + '\n').

3. In the search() function, you are reading the first line of the file (descr = file.readline()) but you are not reading the number of dishes that was written in the write_dishes() function. You can add a line to read the number of dishes before entering the loop. For example: num_dish = int(file.readline()).

4. Inside the search() function, you are comparing descr (which represents the dish name) with search (the ingredient to search for). This comparison will not work correctly because descr only contains the dish name, not the ingredient. To fix this, you should compare ing (the ingredient) with search.

5. In the search() function, you are appending descr (the dish name) to the cookbook list. However, you should be appending ing (the ingredient) to the list instead.

6. After the while loop in the search() function, you are not printing the contents of the cookbook list. You can add a print statement to display the dishes containing the searched ingredient.

Here's the modified code with the mentioned fixes:

Python:
def write_dishes():
    outfile = open('cookbook.txt', 'w')
    num_dish = int(input("Enter number of dish: "))
    outfile.write(str(num_dish) + '\n')
    while num_dish != 0:
        dish = input("Enter a dish: ")
        outfile.write(dish + '\n')
        while True:
            ing = input("Enter ingredients(line-separated): ")
            if ing == "":
                break
            outfile.write(ing + '\n')
        num_dish -= 1
        outfile.write("\n")
    outfile.close()

def search():
    found = False
    search = input('Enter an ingredient: ')
    file = open('cookbook.txt', 'r')
    num_dish = int(file.readline())
    cookbook = []
    for _ in range(num_dish):
        dish = file.readline().rstrip('\n')
        while True:
            ing = file.readline().rstrip('\n')
            if ing == "":
                break
            if search in ing:
                cookbook.append(dish)
                found = True
    file.close()
    if found:
        print('Dishes containing', search + ':')
        for dish in cookbook:
            print(dish)
    else:
        print('No dishes found containing', search + '.')

write_dishes()
search()

With these modifications, the program should now write the dishes and ingredients to the file correctly, and the search functionality should display the dishes containing the searched ingredient.
 
Back
Top