Bottom Page

• 2 Vote(s) - 2.5 Average
• 1
• 2
• 3
• 4
• 5
 Simple IF statement not working as intended gortexxx Unladen Swallow Posts: 2 Threads: 1 Joined: May 2018 Reputation: 0 Likes received: 0 #1 May-17-2018, 06:49 PM 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()``` ThiefOfTime Wafer-Thin Wafer Posts: 89 Threads: 0 Joined: Apr 2018 Reputation: 15 Likes received: 16 #2 May-17-2018, 07:01 PM 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 :) gortexxx Unladen Swallow Posts: 2 Threads: 1 Joined: May 2018 Reputation: 0 Likes received: 0 #3 May-17-2018, 07:54 PM Thank you « Next Oldest | Next Newest »

Top Page

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

Forum Jump:

Users browsing this thread: 1 Guest(s)