3
def main():
    import math
    print('Period of a pendulum')
    Earth_gravity = 9.8
    Mars_gravity = 3.7263
    Jupiter_gravity = 23.12
    print('     ')
    pen = float(input('How long is the pendulum (m)? '))
    if pen < 0:
        print('illegal length, length set to 1')
        pen = 1
        period1 = (2 * 3.14159265359) * math.sqrt(pen / Earth_gravity)
        period2 = (2 * 3.14159265359) * math.sqrt(pen / Mars_gravity)
        period3 = (2 * 3.14159265359) * math.sqrt(pen / Jupiter_gravity)        
        print('     ')
        print('The period is', round(period1,3))
        minutes1 = period1 / 60
        minutes2 = period1 / 60
        minutes3 = period1 / 60
        seconds1 = minutes1 % 60
        seconds2 = minutes2 % 60
        print('or', round(minutes1,1), 'minutes and', seconds, 'seconds on 
Earth')
        print('     ')
        print('The period is', round(period2,3))
        print('or', round(minutes2,1), 'minutes and', seconds, 'seconds on 
Mars')
        print('     ')
        print('The period is', round(period3,3))
        print('or', round(minutes3,1), 'minutes and', seconds, 'seconds on 
Jupiter')        
    else:
        period1 = (2 * 3.14159265359) * math.sqrt(pen / Earth_gravity)
        period2 = (2 * 3.14159265359) * math.sqrt(pen / Mars_gravity)
        period3 = (2 * 3.14159265359) * math.sqrt(pen / Jupiter_gravity)        
        print('     ')
        print('The period is', round(period1,3))
        minutes1 = period1 // 60
        minutes2 = period2 // 60
        minutes3 = period3 // 60
        seconds1 = minutes1 % 60
        seconds2 = minutes2 % 60
        seconds3 = minutes3 % 60
        print('or', round(minutes1,1), 'minutes and', seconds1, 'seconds on 
Earth')
        print('     ')
        print('The period is', round(period2,3))
        print('or', round(minutes2,1), 'minutes and', seconds2, 'seconds on 
Mars')
        print('     ')
        print('The period is', round(period3,3))
        print('or', round(minutes3,1), 'minutes and', seconds3, 'seconds on Jupiter')        

main()

Okay so I need to convert seconds to seconds AND minutes. I am unsure of how to use % to get the seconds and minutes in an output. I need to use // and % in this. I am very new at this so I apologize if it is sloppy or overkill. The area that is messed up are the lines with % included. Thank you!

Patsfan8118
  • 57
  • 2
  • 6
  • `math.pi` is always better than `3.14159265359`. – DYZ Sep 18 '17 at 00:14
  • Possible duplicate of [How does % work in Python?](https://stackoverflow.com/questions/4432208/how-does-work-in-python) – mquantin Sep 18 '17 at 00:28
  • There are already decent answers, but I have noticed that you are using % on minutes, which will not give you seconds. You have to start out with seconds in the first place, so you would modulus on period, not minutes. I would suggest using divmod though. – PoDuck Sep 18 '17 at 02:36

2 Answers2

8

You can simply use divmod which returns the integer divisor and the modulo, a perfect fit for you case:

>>> seconds = 1000
>>> minutes, seconds = divmod(seconds, 60)
>>> hours, minutes = divmod(minutes, 60)
>>> days, hours = divmod(hours, 24)
>>> days, hours, minutes, seconds
(0, 0, 16, 40)

It seems like you only need the first line minutes, seconds = divmod(seconds, 60) but I wanted to showcase how it could be used if there were more conversions too. :)

MSeifert
  • 145,886
  • 38
  • 333
  • 352
3

Integers will round down to the nearest whole number. The %, or modulo, operator reports only the remainder of a division operation.

So 135 % 60 returns 15. This is because 60 goes into 135 twice, but the remaining 15 is less than 60. The two times that 60 goes into 135 is not returned, so you'll need to find that value with a standard division operator.

You can divide by 60 to get minutes, then also use the modulo operator to return the remaining seconds.

time = 135

minutes = time / 60
seconds = time % 60

print minutes
print seconds

returns

2
15
Colin Hancey
  • 219
  • 2
  • 8