嵌套的for循环错误python

时间:2016-07-21 06:06:16

标签: python for-loop

我对编程完全陌生。我试图嵌套我认为是" for-loop"我认为这是一个" for-loop"。每次运行程序时都会出现错误:"程序中出现错误: 意想不到的un&"。老实说,我不知道如何解决它。任何帮助将不胜感激。 这是代码:



import urllib2
import time

stocksToPull  = 'AAPL'
  
def pullData(stock):
    try:
        pricedata = urllib2.urlopen("http://www.google.com/finance/getprices?i=60&p=1d&f=d,o,h,l,c,v&df=cpct&q="+stock+"").read()

        pricevalues = pricedata.split()
        current_price = float(pricevalues[len(pricevalues)-1].split(",")[4]) #High

        pricevalues = pricedata.split()
        Pcurrent_price = float(pricevalues[len(pricevalues)-1].split(",")[2]) #Open

        DPCge= (current_price/Pcurrent_price)/Pcurrent_price #Daily Precent Change

        number = 0.010000000000
# This is the begging of the nested for-loop
        if stock == 'AAPL' and DPCge> number:
        	for eachStock in stocksToPull:
        		stocksToPull  = 'AAPL'
        		pullData(eachStock)

			def pullData(stock):
        			try:
                                    pricedata = urllib2.urlopen("http://www.google.com/finance/getprices?i=60&p=1d&f=d,o,h,l,c,v&df=cpct&q="+stock+"").read()

                                    pricevalues = pricedata.split()
                                    current_price = float(pricevalues[len(pricevalues)-1].split(",")[4]) #High

                                    pricevalues = pricedata.split()
                                    Pcurrent_price = float(pricevalues[len(pricevalues)-1].split(",")[2]) #Open

                                    DPCge= (current_price/Pcurrent_price)/Pcurrent_price #Daily Precent Change

                                    number = 0.010000000000

except Exception,e:
    print'main loop',str(e)

for eachStock in stocksToPull:
    pullData(eachStock)




1 个答案:

答案 0 :(得分:0)

在您的代码中,您有两个try块和一个除外。 except块的缩进与任一try块都不对齐。而且,您已在给定函数中定义了相同的函数!你的代码到处都是。删除内部功能块并将其替换为函数调用(如果这是您需要的)并修复try和except块。