-3
我對編程完全陌生。我試圖在我認爲是「for-loop」的內部嵌套我認爲是「for-loop」的內容。每次運行程序時,我都會收到一個錯誤消息:「程序中有錯誤: 意外的無意」。我真的不知道如何解決它。任何幫助將不勝感激。 這裏是代碼:嵌套的for循環錯誤python
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:
\t for eachStock in stocksToPull:
\t \t stocksToPull = 'AAPL'
\t \t pullData(eachStock)
\t \t \t def pullData(stock):
\t \t \t 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)
如果這是您正在使用的確切代碼,您需要縮進'除了'塊以匹配'try'塊。 – Lafexlos
你的功能也列出了兩次,除非這是你想要的。您的代碼可能會被壓縮 –