Simple IF statement not working as intended - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Simple IF statement not working as intended (/thread-10307.html) |
Simple IF statement not working as intended - gortexxx - May-17-2018 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() RE: Simple IF statement not working as intended - ThiefOfTime - May-17-2018 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 :) RE: Simple IF statement not working as intended - gortexxx - May-17-2018 Thank you |