我对编程完全陌生。我试图把我认为是“for循环”的东西嵌套在我认为是“for循环”的东西里面。每次我运行程序时都会出现一个错误:“您的程序中有一个错误: 意料之外的无知”。我真的不知道该怎么修。任何帮助都将不胜感激。 代码如下:
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)
在代码中,有两个try块和一个except。except块的缩进与try块都不对齐。而且,您已经在给定的函数中定义了相同的函数!你的密码到处都是。删除内部函数块,如果需要,则用函数调用替换它,并修复try和except块。你知道吗
相关问题 更多 >
编程相关推荐