Compare operation in Velocity does not execute True branch when it should - compare

In the operation below TRUE results will not execute the appropriate code branch ($set ($color = red), does anyone know why?
I have tried comparing ($percent_covered < 100), ($percent_covered < "100.000"), ($number.float($percent_covered) < $number.float("100.00")) and ($percent_covered < $number.float("100.00")). I have also tried rearranging the operation but nothing seems to trigger the TRUE statement.
In this example $percent_covered is 18.73
#set ($percent_covered = $math.mul($div, 100))
$percent_covered
#if ($percent_covered < "100.000")
$percent_covered
#set ($color = "red")
#else
#set ($color = "green")
#end
<td class="$color percent">$number.format("#0.00", $percent_covered)%</td>

I had to force the $percent_covered to an integer. To do this I made the following changes. Warning: This is not the most elegant solution, but it worked for me.
#set ($Integer = 0)
#set ($div = $math.div($covered, $requirements.size))
#set ($percent_covered = $math.mul($div, 100))
#*
Create a whole_number, as a string.
*#
#set ($whole_number = $number.format("#0", $percent_covered))
#*
Convert to integer
*#
#if ($Integer.parseInt($whole_number) < 100)
#set ($color = "red")
#else
#set ($color = "green")
#end
<td class="$color percent">$number.format("#0.00", $percent_covered)%</td>

Related

When doing an if statement, it always go into false condition when == is given. It will go into true only if != is given

h = float(input('Enter Height in meter: '))
w = float(input('Enter your weight in Kg: '))
if h == float and w == float:
print(f'BMI = ', w / (h ** 2))
else:
print("Input Error")
always going into false condition while float is entered. but if != is given, it will perform correctly. why is that
if statement should be like this:
if type(h) == float and type(w) == float:
and when you try with != code is will always return false because you are trying to match if 5.6 != float instead you want to try type(5.6) != float

Pine script: Switch trendline based on if statement

How do I set my trendline color and plot based on an if statement i.e. if stock data is > 1 year then use trendline 1 else use trendline method 2:
trend_1 = sma(close, 15)
trend_2 = sma(close, 30)
bearish = true
for i = 0 to trendConfirmationLength - 1
bearish := bearish and (close[i] < trend[i])
bullish = not bearish
// Set the color of the plot based on whether we are bullish (green) or not (red)
c = bullish ? color.green : color.red
// Plot the trend line
trend =
if (len(close) > 252)
trend_1
if (len(close) < 252)
trend_2
trend_plot = plot(trend, title='Trend', color = c)
Thanks
trend_1 = sma(close, 15)
trend_2 = sma(close, 30)
float trend = na
if bar_index >= 252
trend := trend_1
else
trend := trend_2
bool foundBearish = false
bool foundBullish = false
for i = 0 to trendConfirmationLength - 1
if close[i] < trend[i]
foundBearish := true
else if close[i] > trend[i]
foundBullish := true
bullish = foundBullish and not foundBearish
bearish = foundBearish and not foundBullish
neutral = not bullish and not bearish
color c = na
if bullish
c := color.green
else if bearish
c := color.red
else if neutral
c := color.blue
trend_plot = plot(trend, title='Trend', color = c)
If all closes within trendConfirmationLength are below trend then foundBearish will be true and foundBullish will be false (and vice versa). If both foundBearsh and foundBullish are true, it means we had closes both above and below trend.
Alternatively you could do this to keep the trend color until a new trend is formed rather than show a neutral color.
color c = na
if bullish
c := color.green
else if bearish
c := color.red
trend_plot = plot(trend, title='Trend', color = fixnan(c))

Conditionally using distributions in pymc3

In pymc3 how would I create a condition a switch statement around distributions?
""" If True, use mu1, else use mu2? """
with pm.Model() as model_lkj:
category = pm.Categorical('category', p=[0.5, 0.5])
# cat == 1:
mu1 = pm.Normal(name='mu2', mu=5, sd=5)
# else
mu2 = pm.Normal(name='mu2', mu=4, sd=4)

ZeroDivisionError: float division by zero even after using python future module

I am facing an error while running a github code. I think the code is perfect, But i think am facing some dependency issues. Can anyone tell me what could possible be the reason behind this error. I am using python 2.7.
from __future__ import division, print_function
.
.
def time_step(self, xt):
xt = np.reshape(xt, newshape=self.dimensions)
ret_val = 0.
self.buffer.append(xt)
self.present.time_step(xt)
if self.t >= self.buffer_len:
pst_xt = self.buffer[0]
self.past.time_step(pst_xt)
if self.t >= self.present.theta + self.past.theta:
ret_val = self.comparison_function(self.present, self.past,
self.present.alpha)
self.ma_window.append(ret_val)
if self.t % self.ma_recalc_delay == 0:
self.anomaly_mean = bn.nanmean(self.ma_window)
self.anomaly_std = bn.nanstd(self.ma_window, ddof=self.ddof)
if self.anomaly_std is None or self.t < len(self.ma_window):
anomaly_density = 0
else:
normalized_score = (ret_val - self.anomaly_mean)/self.anomaly_std
if -4 <= normalized_score <= 4:
anomaly_density = CDF_TABLE[round(normalized_score, 3)]
elif normalized_score > 4:
anomaly_density = 1.
else:
anomaly_density = 0.
self.t += 1
return ret_val, anomaly_density
The code line which is giving error is the following,
normalized_score = (ret_val - self.anomaly_mean)/self.anomaly_std
Wrap it in try except, I used 0 as except value but you can change it per your needs:
try:
normalized_score = (ret_val - self.anomaly_mean)/self.anomaly_std
except ZeroDivisionError:
normalized_score = 0

Python ignoring while loop with True condition

I'm writing a menu for a simple game, so I thought to use a while loop to let the user choose his wanted option by clicking on it. While my program works properly until this loop, it does not continue when reaching the line of the following loop:
pygame.mouse.set_visible(True) # This is the last line processed.
while True: # This line ist not processed.
(curx,cury)= pygame.mouse.get_pos()
screen.blit(cursor,(curx-17,cury-21))
pygame.display.flip()
# rating values of the cursor position and pressed mouse button below:
(b1,b2,b3) = pygame.mouse.get_pressed() #getting states of mouse buttons
if (b1 == True or b2 == True or b3 == True): # "if one mouse button is pressed"
(cx,cy) = pygame.mouse.get_pos()
if (px <= curx <= px+spx and py <= cury <= py+spy):
return (screen,0)
elif (ox <= curx <= ox+sox and oy <= cury <= oy+soy):
return (screen,1)
elif (cx <= curx <= cx+scx and cy <= cury <= cy+scy):
return (screen,2)
else:
return (screen,3)
time.sleep(0.05)
I have already checked such things like wrong indentation.
BTW my interpreter (python.exe, Python 2.7.11) always does not respond after reaching this line of the
while True:
Because the question was asked in a deleted answer:
I had a print("") between every above shown line to find the problematic line.
As I wrote 4 lines above: The interpreter (and with it the debugger and bug report) has crashed without any further response.
The whole code of this function is:
# MAIN MENU
def smenu (screen,res,menuimg,cursor):
#preset:
x,y = res
click = False
print("preset") # TEST PART
# Fontimports, needed because of non-standard font in use
menu = pygame.image.load("menu.png")
playGame = pygame.image.load("play.png")
options = pygame.image.load("options.png")
crdts = pygame.image.load("credits.png")
print("Fontimport") # TEST PART
#SIZETRANSFORMATIONS
# setting of sizes
smx,smy = int((y/7)*2.889),int(y/7)
spx,spy = int((y/11)*6.5),int(y/11)
sox,soy = int((y/11)*5.056),int(y/11)
scx,scy = int((y/11)*5.056),int(y/11)
print("setting of sizes") # TEST PART
# setting real size of text 'n' stuff
menu = pygame.transform.scale(menu,(smx,smy))
playGame = pygame.transform.scale(playGame,(spx,spy))
options = pygame.transform.scale(options, (sox,soy))
crdts = pygame.transform.scale(crdts, (scx,scy))
cursor = pygame.transform.scale(cursor,(41,33))
print("actual size transformation") # TEST PART
#DISPLAY OF MENU
# fixing positions
mx, my = int((x/2)-((y/7)/2)*2.889),10 # position: 1. centered (x) 2. moved to the left for half of the text's length 3. positioned to the top(y), 10 pixels from edge
px, py = int((x/2)-((y/11)/2)*6.5),int(y/7+10+y/10) # position: x like above, y: upper edge -"menu"'s height, -10, - height/10
ox, oy = int((x/2)-((y/11)/2)*5.056),int(y/7+10+2*y/10+y/11)
cx, cy = int((x/2)-((y/11)/2)*5.056),int(y/7+10+3*y/10+2*y/11)
print("fixing positions") # TEST PART
# set to display
#screen.fill(0,0,0)
screen.blit(menuimg,(0,0))
screen.blit(menu,(mx,my))
screen.blit(playGame,(px,py))
screen.blit(options,(ox,oy))
screen.blit(crdts,(cx,cy))
pygame.display.flip()
print("set to display") # TEST PART
# request for input (choice of menu options)
pygame.mouse.set_visible(True)
print("mouse visible") # TEST PART last processed line
while (True):
print("While-loop") # TEST PART
curx,cury = pygame.mouse.get_pos()
screen.blit(cursor,(curx-17,cury-21))
pygame.display.flip()
# decision value below
(b1,b2,b3) = pygame.mouse.get_pressed() # getting mouse button's state
if (b1 == True or b2 == True or b3 == True): # condition true if a buton is pressed
(cx,cy) = pygame.mouse.get_pos()
if (px <= curx <= px+spx and py <= cury <= py+spy):
return (screen,0)
elif (ox <= curx <= ox+sox and oy <= cury <= oy+soy):
return (screen,1)
elif (cx <= curx <= cx+scx and cy <= cury <= cy+scy):
return (screen,2)
else:
return (screen,3)
time.sleep(0.05)
print("directly skipped")
Would comment but can't because of low rep.
The problem might be, that you're returning a value to a while loop outside a function.
Although it's a bit weird that your interpreter/IDE isn't giving you the correct Error message for it.
I think that the problem is in the following line of your code:
if (b1 == True or b2 == True or b3 == True):
This condition never becomes true so you are stuck into the while loop without your function returning anything.