python - Calling a Function within a secondary Function, or calling a Function defined within a larger Function -
i'm trying use python create small program, show current prices of theoretical portfolio, , offer option refresh portfolio, or new quote of choice.
i can work in program, problem i'm having defined functions.
if @ run_price1(), you'll notice identical of run_price(); run_price() located within update function.
if take out of update function, update function doesn't work. if don't list somewhere outside of update function, later user input doesn't work.
the question: looking either way call function defined within function, or way use defined function inside of secondary function.
my code:
import mechanize bs4 import beautifulsoup def run_price1(): mybrowser = mechanize.browser() htmlpage=mybrowser.open(web_address) htmltext=htmlpage.get_data() mysoup = beautifulsoup(htmltext) mytags = mysoup.find_all("span", id=tag_id) myprice = mytags[0].string print"the current price of, {} is: {}".format(ticker.upper(), myprice) def update(): my_stocks = ["aapl","goog","sne","msft","spy","trgt","petm","fslr","fb","f","t"] counter = 0 while counter < len(my_stocks): web_address = "http://finance.yahoo.com/q?s={}".format(my_stocks[counter]) ticker = my_stocks[counter] #'yfs_l84_yhoo' - 1(one) lowercase "l" tag_id = "yfs_l84_{}".format(ticker.lower()) def run_price(): mybrowser = mechanize.browser() htmlpage=mybrowser.open(web_address) htmltext=htmlpage.get_data() mysoup = beautifulsoup(htmltext) mytags = mysoup.find_all("span", id=tag_id) myprice = mytags[0].string print"the current price of, {} is: {}".format(ticker.upper(), myprice) run_price() counter=counter+1 update() ticker = "" while ticker != "end": ticker = raw_input("type 'update', rerun portfolio, 'end' stop program, or lowercase ticker see price: ") web_address = "http://finance.yahoo.com/q?s={}".format(ticker.lower()) tag_id = "yfs_l84_{}".format(ticker.lower()) if ticker == "end": print"good bye" elif ticker == "update": update() else: run_price1()
you can call run_price1()
update()
function, call run_price
.
functions defined @ top of module global in module, other functions can refer name , call them.
any value function needs does need passed in argument:
def run_price1(web_address, tag_id): # ... def update(): my_stocks = ["aapl","goog","sne","msft","spy","trgt","petm","fslr","fb","f","t"] counter = 0 while counter < len(my_stocks): web_address = "http://finance.yahoo.com/q?s={}".format(my_stocks[counter]) ticker = my_stocks[counter] #'yfs_l84_yhoo' - 1(one) lowercase "l" tag_id = "yfs_l84_{}".format(ticker.lower()) run_price1(web_address, tag_id) counter=counter+1
Comments
Post a Comment