Bottom Page

Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Simple IF statement not working as intended
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)
        if userSeconds >=3600:
            finalHours, finalMinutes
            #print(userSeconds,' seconds into hours is: ',secondsToHours)
            if userSeconds >=60:
                #print(userSeconds,' seconds into minutes is: ',secondsToMinutes)

#Call the TimeCalc Function
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 :)
Thank you

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Else Statement Not Working SenkouSimmer 4 439 Jul-22-2019, 11:42 AM
Last Post: jefsummers
  Simple Eight-Puzzle not working! BenjaminDJ 2 940 May-04-2018, 12:17 PM
Last Post: BenjaminDJ
  For and If Loop not working as intended Jaykin 1 792 Apr-29-2018, 01:14 PM
Last Post: j.crater
  help! if statement not working molliemae 2 655 Apr-26-2018, 11:13 PM
Last Post: woooee
  Function not working as intended I think? TimeForged 2 999 Mar-11-2018, 09:05 AM
Last Post: buran
  [split] Function not working as intended mihshyahoocom 1 698 Mar-11-2018, 09:04 AM
Last Post: buran
  Supposedly simple program not working! Please help! BenjaminDJ 5 1,169 Feb-20-2018, 08:43 PM
Last Post: BenjaminDJ
  Simple Python program not working AudioKev 3 1,039 Jan-23-2018, 09:57 PM
Last Post: Larz60+

Forum Jump:

Users browsing this thread: 1 Guest(s)