我在python工作中有一个半工作决策循环,但需要一个指针让它100%工作。
我有几个股票代码我更新了每个' x'分钟并在Raspberry Pi LCD上显示该值。我试图做的并且失败的是,当其中一只股票下跌时,背光变为红色,如果两者都是负数,如果最后一个是负数,但如果第一个是负数,那么第二个是正数价值它不起作用。我怎样才能做到这一点?
tickersymbols = ['ticker1', 'ticker2']
i = 0
while (i < 3):
lcd.clear()
for tickerSymbol in tickersymbols:
allInfo = ystockquote.get_all(tickerSymbol)
price = float(allInfo["price"])
change = float(allInfo["change"])
if change >= 0:
backlight.rgb(0,255,0)
lcd.write(tickerSymbol + "= %+3.2f (%+0.2f)" % (price, change))
else:
backlight.rgb(255,0,0)
lcd.write(tickerSymbol + "= %+3.2f (%+0.2f)" % (price, change))
答案 0 :(得分:0)
根据您的评论,最简单的方法是在for循环之前设置backlight.rgb(0, 255, 0)
,如果change
是&lt; ticker_symbols = ['ticker1', 'ticker2']
while i < 3:
lcd.clear()
backlight.rgb(0, 255, 0) # reset normal background each time
for ticker_symbol in ticker_symbols:
all_info = ystockquote.get_all(ticker_symbol)
price = float(all_symbol["price"])
change = float(all_symbol["change"])
if change < 0: # only change to red when we find any price drop
backlight.rgb(255, 0, 0)
lcd.write(ticker_symbol + "= %+3.2f (%+0.2f)" % (price, change)) # always change display
,则只需更改为红色。 0:
backlight.rgb(0, 255, 0)
始终以{{1}}开头然后更改为红色以通知价格下降是有意义的。否则,通过查看屏幕,您是否知道屏幕是否是前一次掉落或当前的红色。
答案 1 :(得分:0)
这非常简陋,但为什么不在if
,中添加另一个条件语句(只要您的默认颜色不是红色)。这样,您的for
循环仍将继续运行,您仍然可以获得此时所做的所有lcd.write
循环。
tickersymbols = ['ticker1', 'ticker2']
i = 0
while (i < 3):
lcd.clear()
backlight.rgb(255,0,0)
for tickerSymbol in tickersymbols:
allInfo = ystockquote.get_all(tickerSymbol)
price = float(allInfo["price"])
change = float(allInfo["change"])
if change < 0:
backlight.rgb(0,255,0)
lcd.write(tickerSymbol + "= %+3.2f (%+0.2f)" % (price, change))
答案 2 :(得分:0)
在仔细查看您的问题之后,您不想从for循环中解脱。这将导致您错过剩余tickerSymbols的lcd.write()语句。
相反,如果您发现价格下降,您只想设置一个标志。然后,在循环之后,根据标志更改LED的颜色。
tickersymbols = ['ticker1', 'ticker2']
i = 0
while (i < 3):
lcd.clear()
stockWentDown=False
for tickerSymbol in tickersymbols:
allInfo = ystockquote.get_all(tickerSymbol)
price = float(allInfo["price"])
change = float(allInfo["change"])
lcd.write(tickerSymbol+ "= %+3.2f (%+0.2f)" % (price, change))
if change < 0:
stockWentDown=True
if not stockWentDown: #if the stock went up or stayed the same
#color it green
backlight.rgb(0,255,0)
elif stockWentDown: #if the stock went down
#color it red
backlight.rgb(255,0,0)
答案 3 :(得分:0)
根据您的代码,如果第一次更改为否定,第二次更改不会覆盖显示。在我看来,只要 Padraic Cunningham 突出显示更改值,就可以中断。但是,如果要保持两次运行while循环的配置,则始终可以设置标志
tickersymbols = ['ticker1', 'ticker2']
i = 0
flag=0
while (i < 3):
lcd.clear()
for tickerSymbol in tickersymbols:
allInfo = ystockquote.get_all(tickerSymbol)
price = float(allInfo["price"])
change = float(allInfo["change"])
if (change >= 0 && flag==0):
backlight.rgb(0,255,0)
lcd.write(tickerSymbol + "= %+3.2f (%+0.2f)" % (price, change))
else:
backlight.rgb(255,0,0)
lcd.write(tickerSymbol + "= %+3.2f (%+0.2f)" % (price, change))
flag=1