-3

Thats my code below, i am quite new so i am sure its just an amateur mistake on my side

def availability():
    r = requests.get('https://planet54.com/products/strappy-casual-dress-black.json')
    blackdress =json.loads((r.text))['product']['title']
    productname = blackdress

      for blackdress in productname:

       if productname == 'Strappy Casual Dress - Black':
            producturl = 'https://planet54.com/products/strappy-casual-dress-black'

          return producturl

    return False 
DataPapi
  • 3
  • 3
  • 1
    python uses indentation to define code blocks. there for you cant mix and match your indentation it needs to be consistent – Chris Doyle Nov 26 '19 at 23:41
  • Your for loop is indented for no reason it seems. – lbragile Nov 26 '19 at 23:44
  • 1
    Also the indentation inside the `if` statement is inconsistent. – kaya3 Nov 26 '19 at 23:44
  • Also your first return is positioned improperly according to the statement in the if block. Python relies on proper indents to avoid using { } or end if statements as in other languages. If you don't have proper indents, Python has no idea what to do with your code. On that note, I assume you want to check every single product name and see if it matches the condition you posed in the if statement. If so, check my answer. – lbragile Nov 26 '19 at 23:51
  • I recommend (1) taking a look at the [PEP8 docs on Indentation](https://www.python.org/dev/peps/pep-0008/#indentation), (2) using an editor or an IDE that detects and displays possible indentation errors on your code (even before you run it) – Gino Mempin Nov 27 '19 at 04:26

2 Answers2

3

Unlike other languages where whitespace is ignored, it has a very specific function in python. Python language uses indentation to nest and define code structure and code blocks. So you need to ensure your indentation is consistent in your code.

def availability():
    r = requests.get('https://planet54.com/products/strappy-casual-dress-black.json')
    blackdress = json.loads((r.text))['product']['title']
    productname = blackdress

    for blackdress in productname:
        if productname == 'Strappy Casual Dress - Black':
            producturl = 'https://planet54.com/products/strappy-casual-dress-black'
            return producturl
    return False

Chris Doyle
  • 10,703
  • 2
  • 23
  • 42
  • This will "return" after the first loop regardless of the condition. I don't think this is what OP wanted, they probably wanted to loop through the entire `productname` variable and see if ANY match a certain name, not just the first product. – lbragile Nov 26 '19 at 23:49
  • 1
    Good spot. Indented the return inside the if – Chris Doyle Nov 26 '19 at 23:53
2

This seems to be what you want.

def availability():
  r = requests.get('https://planet54.com/products/strappy-casual-dress-black.json')
  blackdress =json.loads((r.text))['product']['title']
  productname = blackdress

  for blackdress in productname:
    if productname == 'Strappy Casual Dress - Black':
      producturl = 'https://planet54.com/products/strappy-casual-dress-black'
      return producturl

  return False 

Although note that your return inside the if statement will cause the for-loop to quit early if the condition is found.

lbragile
  • 7,549
  • 3
  • 27
  • 64