Python Forum
problems with the If statement or is it the variables being used - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: problems with the If statement or is it the variables being used (/thread-16963.html)



problems with the If statement or is it the variables being used - NickIgoe - Mar-22-2019

I am very new to python but do have some coding experience but for the life of me cannot get my head around this issue I am having.

When I hard code a number into my IF statement the code runs fine without any issues as shown below.

   
                average = totaltime / count
                if average > 5:
# print with a red background
                        print ("\033[1;37;41m\r")
                        print round(average,2), "Average load rate"
                        print ("\033[1;37;40m\r")
                else:
# print with a green background
                        print ("\033[1;37;42m\r")
                        print round(average,2), "Average load rate"
                        print ("\033[1;37;40m\r")
        print ("\033[1;37;44m  WAITING FOR TAG  \n")
        print ("\033[1;37;40m\r")
However if I replace the 5 in the if statement with a variable which was set by an input statement the code always runs through the else statement regardless of the value entered.

target = raw_input('Target time in seconds: ')
print round(timeC,2), "Seconds", (target),"Target"
                if average > target:
# print with a red background
                        print ("\033[1;37;41m\r")
                        print round(average,2), "Average load rate"
                        print ("\033[1;37;40m\r")
                else:
# print with a green background
                        print ("\033[1;37;42m\r")
                        print round(average,2), "Average load rate"
                        print ("\033[1;37;40m\r")
        print ("\033[1;37;44m  WAITING FOR TAG  \n")
        print ("\033[1;37;40m\r")
I have tried print out the values before the IF statement to check the values and ensure another part of the code is having an effect. I have also found an online debugger and ran through the code and no matter what I did the code ran fine. However when I run the code on my RaspPi it always defaults to the ELSE when I change the code to a variable. I have tried to INT the variables, ive put the statement in brackets. I am now pulling my hair out.


RE: problems with the If statement or is it the variables being used - Yoriz - Mar-22-2019

raw_input returns values as a string so you need to change the string to a number.
target = int(target)



RE: problems with the If statement or is it the variables being used - NickIgoe - Mar-22-2019

Many thanks, you method worked perfectly!

I thought I had done that when I tried.

If int(average > target):