Python Forum
Simple IF statement not working as intended
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple IF statement not working as intended
#1
This is early work in my book, so should be simple. I went and came up with some functions there, called finalDays, finalHours, and finalMinutes.

As you can see, I put them in the IF statements, intending them to work as I wish.

For some reason, the output from Python is always all three of them. So if I enter in a number less than 86400, or even less than 3600, I still get all three functions returned. What am I missing?



def TimeCalc():
    #get the user to input the seconds
    userSeconds = int(input('Enter in the number of seconds to convert: '))

    #create functions that will convert seconds into desired time period
    secondsToMinutes = round(userSeconds/60,4)
    secondsToHours = round(userSeconds/3600,4)
    secondsToDays = round(userSeconds/86400,4)

    #testing more functions. These are for the IF statements if I have to repeat them
    finalDays = print(userSeconds,' seconds into days is: ',secondsToDays)
    finalHours = print(userSeconds,' seconds into hours is: ',secondsToHours)
    finalMinutes = print(userSeconds,' seconds into minutes is: ',secondsToMinutes)

    #begin calculating the IF Then statements with the inputs and functions
    if userSeconds >= 86400:
        finalDays, finalHours, finalMinutes
        #print(userSeconds,' seconds into days is: ',secondsToDays)
    else:
        if userSeconds >=3600:
            finalHours, finalMinutes
            #print(userSeconds,' seconds into hours is: ',secondsToHours)
        else:
            if userSeconds >=60:
                finalMinutes
                #print(userSeconds,' seconds into minutes is: ',secondsToMinutes)

#Call the TimeCalc Function
TimeCalc()
Reply
#2
That is because you try to save the print function into the variables, but what happens is that the return value of the function (in this case None) will be saved to the variables. So the print functions are all executed before you reache the if cases. Just put them in the if cases and it should be fine :)
Reply
#3
Thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  if statement not working g0g0g1g 2 1,607 Sep-08-2020, 05:40 PM
Last Post: nilamo
  Else Statement Not Working SenkouSimmer 4 3,176 Jul-22-2019, 11:42 AM
Last Post: jefsummers
  Simple Eight-Puzzle not working! BenjaminDJ 2 3,131 May-04-2018, 12:17 PM
Last Post: BenjaminDJ
  For and If Loop not working as intended Jaykin 1 2,564 Apr-29-2018, 01:14 PM
Last Post: j.crater
  help! if statement not working molliemae 2 2,553 Apr-26-2018, 11:13 PM
Last Post: woooee
  Function not working as intended I think? TimeForged 2 2,997 Mar-11-2018, 09:05 AM
Last Post: buran
  [split] Function not working as intended mihshyahoocom 1 2,077 Mar-11-2018, 09:04 AM
Last Post: buran
  Supposedly simple program not working! Please help! BenjaminDJ 5 3,995 Feb-20-2018, 08:43 PM
Last Post: BenjaminDJ
  Simple Python program not working AudioKev 3 3,170 Jan-23-2018, 09:57 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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