Python Forum
Script get's really weird error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Script get's really weird error
#1
Shocked 
Hello,
I am trying to write a Python script to ask the user for a recipe. The script should look up the ingredients needed for the recipe via OpenAI. Then, it should look up the live prices at Albert Heijn, at their site. However, I'm reveiving an error:
Error:
File "<stdin>", line 1 ingredient_prices = get_ingredient_prices(ingredients) IndentationError: unexpected indent >>> total_cost = sum(ingredient_prices.values()) File "<stdin>", line 1 total_cost = sum(ingredient_prices.values()) IndentationError: unexpected indent
It looks good to me tho. Here's the full code:

import requests
from bs4 import BeautifulSoup
import openai

openai.api_key = '*************'

def get_recipe(dish_name):
    prompt = f"Geef me een recept voor {dish_name} inclusief een lijst van ingrediënten."
    response = openai.Completion.create(
        engine="text-davinci-003",
        prompt=prompt,
        max_tokens=150
    )
    return response.choices[0].text.strip()

def search_ingredient_price(ingredient):
    search_url = f"https://www.ah.nl/zoeken?query={ingredient.replace(' ', '%20')}"
    response = requests.get(search_url)
    soup = BeautifulSoup(response.text, 'html.parser')
    
   
    try:
        product = soup.find('div', class_='product-card-portrait')
        if product:
            price = product.find('span', class_='price-amount').text.strip().replace(',', '.').replace('€', '')
            return float(price)
        else:
            return 0.0
    except AttributeError:
        return 0.0

def get_ingredient_prices(ingredients):
    prices = {}
    for ingredient in ingredients:
        prices[ingredient] = search_ingredient_price(ingredient)
    return prices

def main():
    dish_name = input("Voer uw gewenste gerecht in: ")
    recipe = get_recipe(dish_name)
    print("\nRecept voor", dish_name)
    print(recipe)
    

    ingredients = [line.split(' ')[-1] for line in recipe.split('\n') if line.strip()]

    ingredient_prices = get_ingredient_prices(ingredients)
    total_cost = sum(ingredient_prices.values())

    if total_cost < 17:
        print("\nDe geschatte totale kosten voor de ingrediënten zijn €", total_cost)
        for ingredient, price in ingredient_prices.items():
            print(f"{ingredient}: €{price}")
    else:
        print("\nHet totale bedrag voor de ingrediënten overschrijdt €17,-. Probeer een ander gerecht.")

if __name__ == "__main__":
    main()
Please helpppp :)
deanhystad write May-25-2024, 08:03 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply


Messages In This Thread
Script get's really weird error - by DaJohn - May-25-2024, 06:47 PM
RE: Script get's really weird error - by DaJohn - May-25-2024, 07:20 PM
RE: Script get's really weird error - by deanhystad - May-25-2024, 08:07 PM
RE: Script get's really weird error - by DaJohn - May-25-2024, 09:08 PM
RE: Script get's really weird error - by deanhystad - May-25-2024, 09:11 PM
RE: Script get's really weird error - by Pedroski55 - May-26-2024, 05:05 AM
RE: Script get's really weird error - by snippsat - May-26-2024, 09:10 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Weird function defaults error? wallgraffiti 5 2,393 Aug-07-2020, 05:55 PM
Last Post: deanhystad
  Weird SQLAchemy connection string error pawpaw 0 1,591 Jun-28-2020, 10:11 AM
Last Post: pawpaw
  [split] Python beginner: Weird Syntax Error mnsaathvika 1 2,238 Jul-22-2019, 06:14 AM
Last Post: buran
  Weird scoping error Stef 3 3,008 Jan-20-2019, 04:36 PM
Last Post: Stef
  Weird error in pycharm TheRedFedora 1 2,787 Mar-11-2018, 09:01 PM
Last Post: Larz60+
  weird error in random sentence generator bobger 9 5,940 Nov-29-2017, 07:34 PM
Last Post: bobger
  Error Handling is weird PythonAndArduino 1 3,100 Nov-09-2017, 05:08 AM
Last Post: Mekire
  Python beginner: Weird Syntax Error mentoly 5 10,544 Oct-13-2017, 08:06 AM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020