0

I am getting -- " unindent does not match any outer indentation level" while coding in browser online for repl. It's showing error in prices = {} Here is my code:

if __name__ == "__main__":

    # Query the price once every N seconds.
    for _ in iter(range(N)):
        quotes = json.loads(urllib.request.urlopen(QUERY.format(random.random())).read())
        prices = {}
        for quote in quotes:
            stock, bid_price, ask_price, price = getDataPoint(quote)
      prices[stock] = price
            print ("Quoted %s at (bid:%s, ask:%s, price:%s)" % (stock, bid_price, ask_price, price))

        print ("Ratio %s" % getRatio(prices['ABC', prices['DEF']))
quamrana
  • 37,849
  • 12
  • 53
  • 71
Pankaj Pandey
  • 49
  • 2
  • 8
  • 3
    Indentation *means something* in Python. You can't position your code horizontally however you want, you have to use it to indicate the structure of your program - it's the only want to indicate where the end of a loop or function is, for example. – jasonharper Jun 23 '20 at 15:42
  • You have tabs for the first two lines that are indented, and then just spaces for the `prices = {}` line. You must make your indents uniform. – quamrana Jun 23 '20 at 15:42
  • @tdelaney Block, not block scope. Python doesn't have block scopes. – chepner Jun 23 '20 at 15:43
  • @chepner - its "suite" really, but that's a bit obscure. I'll delete the comment anyway. – tdelaney Jun 23 '20 at 15:56
  • yea. thanks ! It's working now. – Pankaj Pandey Jun 23 '20 at 16:00

1 Answers1

1

The problem is at line 7

prices[stock] = price

This particular line. As this line is not indented with the second loop. Since python makes use of procedural language, if you miss out on adding tabs or spaces between your lines of code, then you will most likely experience this error. ... While programming you haven't indented the prices[stock] = price line.

Updated Code-

# Query the price once every N seconds.
for _ in iter(range(N)):
    quotes = json.loads(urllib.request.urlopen(QUERY.format(random.random())).read())
    prices = {}
    for quote in quotes:
        stock, bid_price, ask_price, price = getDataPoint(quote)
        prices[stock] = price
        print ("Quoted %s at (bid:%s, ask:%s, price:%s)" % (stock, bid_price, ask_price, price))

    print ("Ratio %s" % getRatio(prices['ABC', prices['DEF']))

In case you don't want to change the value of prices[stock] inside the second loop, just paste that after the print ("Quoted %s at (bid:%s, ask:%s, price:%s)" % (stock, bid_price, ask_price, price)) line and indent it according to the first loop.

I hope this helped...

Ayush Jain
  • 154
  • 1
  • 7