Python Forum
problems with the If statement or is it the variables being used
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problems with the If statement or is it the variables being used
#1
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.
Reply
#2
raw_input returns values as a string so you need to change the string to a number.
target = int(target)
Reply
#3
Many thanks, you method worked perfectly!

I thought I had done that when I tried.

If int(average > target):
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Problems with variables in multithreading Wombaz 2 1,320 Mar-08-2022, 03:32 PM
Last Post: Wombaz
  Problems with if / else statement droid206 6 2,738 Apr-19-2020, 08:06 AM
Last Post: astral_travel
  Quotes and variables in print statement Mark17 4 3,038 Sep-13-2019, 04:07 PM
Last Post: Mark17
  Having problems using 'or' in a 'if' statement? umut3806 2 2,093 Jul-21-2019, 11:33 PM
Last Post: umut3806

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020