股票对象实例
class Stock(object): def __init__(self,stockCode ,stockName,averagePrice_yesterday,averagePrice_today,position_old): self.stockCode=stockCode self.stockName=stockName self.averagePrice_yesterday=averagePrice_yesterday self.averagePrice_today=averagePrice_today self.position_old=position_old def displayStock(self): print (("股票名称:%s"+" "+"股票代码:%s"+" "+"昨日均价:%s"+" "+"今日均价:%s"+" "+"昨日仓位:%s")% (self.stockName,self.stockCode,self.averagePrice_yesterday,self.averagePrice_today,self.position_old)) def profit(self): stock_profit=(self.averagePrice_today-self.averagePrice_yesterday)/self.averagePrice_yesterday return stock_profit
输入测试数据:
stock1=Stock("600036","招商银行",18.06,17.81,0.1)stock2=Stock("002307","北新路桥",20.812,19.177,0.2)stock1.displayStock()stock2.displayStock()
运行结果:
股票名称:招商银行 股票代码:600036 昨日均价:18.06 今日均价:17.81 昨日仓位:0.1
股票名称:北新路桥 股票代码:002307 昨日均价:20.812 今日均价:19.177 昨日仓位:0.2-0.0138427464009-0.0785604458966