Question: Hey, can someone help me with the code below in python? I need to get: To calculate the distance between the planets Enter two numbers

Hey, can someone help me with the code below in python? I need to get:

"To calculate the distance between the planets

Enter two numbers or 0 to quit"

#Enter the first planet

#Enter the second planet

Then the console is:

Name of the planet1 and name of the planet2 are on average distance(eg 20 million miles) miles apart

code here

Please include brief explanations if you can

DASH_LENGTH = 60 COLUMN_LENGTH = 25 planets = ( ('Mercury', 57), ('Venus', 108), ('Earth', 150), ('Mars', 228), ('Jupiter', 779), ('Saturn', 1430), ('Uranus', 2880), ('Neptune', 4500) ) def get_integer_input(message, min_num = 0, max_num = 0): """ Get a valid integer value from the user. If the min and max value are both zero then they are enforced ranges :param message: user's prompt :param min_num: min integer input value allowed :param max_num: max integer input value allowed :return: user input """ while True: # Keep looping until the user enters a valid value # prevent the program from ending due to valueError runtime error try: user_input = int(input(message)) # if this fails the except block will catch the error if min_num == 0 and max_num == 0: # if there are not no min and max values set print(user_input) elif min_num <= user_input <= max_num: print(user_input) else: print(f"\tInvalid Input: Please enter a number between {min_num} and {max_num}.") continue # To try playing the game again except ValueError: # When the user enter a non integer value print("\tInvalid Input: Please enter a number. ") continue # try again # Define a function to calculate the distance between two planets def calculate_distance(planet1_num, planet2_num): # function level docstring planet1_info = planets[planet1_num - 1] planet1_name, planet1_dist = planet1_info planet2_info = planets[planet2_num - 1] planet2_name, planet2_dist = planet2_info absolute_distance = abs(planet1_num - planet2_num) print(planet1_info, "and", planet2_info, "are on average", absolute_distance, "million miles apart.") def display_planets_menu(): # function level docstring print('=' * DASH_LENGTH) print(" Planet's Average Distance From the Sun ") print('=' * DASH_LENGTH) menu_num = 1 for planet, distance in planets: print(f"#{menu_num} {planets:<7} = {distance:>4} millions miles") menu_num += 1 # Main loop to repeatedly ask the user for two planets and print the distance between them def main(): # function level docstring display_planets_menu() print("Enter two planet numbers or 0 to quit:") while True: planet1 = get_integer_input("Please enter the 1st Planet #", min_num = 0, max_num =len(planets)) while True: planet2 = get_integer_input("Please enter the 2nd Planet #", min_num = 0, max_num = len(planets)) # Check if the same planet was entered twice if planet1 == planet2 : print("Invalid Input: The same planet was entered twice.") else: break if planet1 == '0': break if planet2 == 0 : break calculate_distance(planet1_num, planet2_num) # Call the calculate_distance function and print the result result = calculate_distance(planet1, planet2) print(result) input("Press enter to continue...") print('=' * DASH_LENGTH) print( " Nice job!") print('=' * DASH_LENGTH) # if this is starting module then return the main function if __name__ == "__main__": main() 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!