# Distance.py Copyright (c) Kari Laitinen # http://www.naturalprogramming.com # 2006-05-28 File created. # 2022-12-16 Converted to Python 3. # Python 'understands' the same format specifiers (e.g. %f) # as the Java printf() method and the C prinf() function. # Operator % is used between the string that contains # the format specifier and the value that will be put # in place of the format specifier. (In this use % can be # called the string formatting operator. % is also the # remainder (modulo) operator in Python.) # If there are several format specifiers in a string, # the corresponding values must be given inside # parentheses. A list of values inside parentheses form # a tuple. print( "\n This program converts meters to other units of" \ "\n distance. Please, enter a distance in meters: ", end=" " ) distance_in_meters = float( input() ) distance_in_kilometers = distance_in_meters / 1000.0 distance_in_miles = 6.21371e-4 * distance_in_meters distance_in_yards = 1.093613 * distance_in_meters distance_in_feet = 3.280840 * distance_in_meters distance_in_inches = 12 * distance_in_feet distance_in_light_years = distance_in_meters / \ ( 2.99793e8 * 365 * 24 * 3600 ) print( "\n %f meters is: \n" % distance_in_meters, end="" ) print( "%15.3f kilometers\n" % distance_in_kilometers, end="" ) print( "%15.3f miles \n" % distance_in_miles, end="" ) print( "%15.3f yards \n" % distance_in_yards, end="" ) # The following statement occupies two program lines. A backslash # is not needed here because a comma separated list of values # may continue on the following line. print( "%15.3f feet \n%15.3f inches \n" % ( distance_in_feet, distance_in_inches ), end="" ) print( "%15.5e light years \n" % distance_in_light_years )