【问题标题】:Python: Running multi variables using the same function at the same time. New to PythonPython:同时使用相同的函数运行多个变量。 Python 新手
【发布时间】:2016-03-29 21:11:53
【问题描述】:

过去几天我一直在使用 python,并开始从事一个项目。目前试图弄清楚如何使用多个变量(在本例中为股票代码)执行相同的函数。最好用一个 input() 用逗号或其他东西分隔。不过,我在最后一部分遇到了障碍。谁能指出我下一步要去哪里? (同时运行具有多个变量的同一个函数。)

这是我的代码:

#Google + Yahoo Finance Stock Lookup
from googlefinance import getQuotes
from yahoo_finance import Share
import googlefinance
import datetime, time
import os
from datetime import datetime


tDate = datetime.now().strftime('%Y-%m-%d')
print (tDate)
tDateConv = str(tDate)

try:
    os.chdir('/Users/Jakes_Macbook/Desktop/Python/Stocks')
except Exception:
    print('Default Path does not exsist, make sure your directory is   right.')
    pass

run = True
while run == True:
    print('You are currently storing the file in ')
    print(os.getcwd())
    print('type "yes" to continue')
confirm = input()
if confirm == 'yes':
    print ('ok\n')
    try:
        os.makedirs(tDateConv)
    except Exception:
        pass
    os.chdir(tDateConv)
    print('File will be saved to:')
    print(os.getcwd())
    break
else:
    print('Where do you want to store the file?')
    changeDir = input()
    os.chdir(changeDir)


print('What Stock or Stocks would you like to look up?')
stockSymbol = input()

def runStocks():

    print (" ")
    print ("Stock Symbol: " + stockSymbol)
    stockSymbolYhoo = Share(stockSymbol)

    stockFile = open(str(stockSymbol)+'.txt', 'a')

    dicStored = googlefinance.getQuotes(stockSymbol)[0]
    numStoredPrice = float(dicStored['LastTradePrice'])
    print('Stock Open: ' + stockSymbolYhoo.get_open())
    print ("Stored Price: " + str(numStoredPrice))
    stockFile.write(str("\n" + "Stock Symbol: " + stockSymbol + "\n"))
    stockFile.write(str("\n" + "Open Price: " + stockSymbolYhoo.get_open() + "\n"))
    stockFile.write(str("Stored Price: " + str(numStoredPrice)+'\n'))


    runs = 0
while runs < 5:
    stor = googlefinance.getQuotes(stockSymbol)[0]
    price = stor['LastTradePrice']
    print(str(datetime.now().strftime('%Y-%m-%d %H:%M:%S')) + " | " + price)
    stockFile.write(str(datetime.now().strftime('%Y-%m-%d %H:%M:%S')) + " | Price " + price + ' \n')

    numPrice = float(price)

    if numPrice < numStoredPrice*float(.995):
        print ("buy")

    time.sleep(5)
    runs = runs + 1

stockFile.close()

runStocks()

我的目标是让输入的每个股票代码在今天的文件夹中创建自己的文件。我很确定一旦我获得了多个功能,我就能弄清楚如何做到这一点。提前致谢。

另外,如果您有任何重要的建议或最佳做法,请告诉我。这就像我使用 python 的第二天。再次感谢。

【问题讨论】:

    标签: python function finance yahoo-finance google-finance


    【解决方案1】:

    只需将它们传递给函数:

    # Note the definition was updated to be able to pass in a stockSymbol
    def runStocks(stockSymbol):
    
        print (" ")
        print ("Stock Symbol: " + stockSymbol)
        stockSymbolYhoo = Share(stockSymbol)
    
        stockFile = open(str(stockSymbol)+'.txt', 'a')
    
        dicStored = googlefinance.getQuotes(stockSymbol)[0]
        numStoredPrice = float(dicStored['LastTradePrice'])
        print('Stock Open: ' + stockSymbolYhoo.get_open())
        print ("Stored Price: " + str(numStoredPrice))
        stockFile.write(str("\n" + "Stock Symbol: " + stockSymbol + "\n"))
        stockFile.write(str("\n" + "Open Price: " + stockSymbolYhoo.get_open() + "\n"))
        stockFile.write(str("Stored Price: " + str(numStoredPrice)+'\n'))
    
    stockSymbols = input("Enter stock symbols separated by commas").split(",")
    
    for stockSymbol in stockSymbols:
        runStocks(stockSymbol) # Call your function in a loop
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-16
      • 2019-02-11
      相关资源
      最近更新 更多