Question: You will begin by creating the modules that hold the conversion functions. Start a new Python file and build each function to do the appropriate
You will begin by creating the modules that hold the conversion functions. Start a new Python file and build each function to do the appropriate conversions.
Create a module named miles.
Create a function within this module named convertToMiles that accepts one value and returns the converted value when called.
Create a second module named kilometers.
Create a function within this module named convertToKilometers that accepts one value and returns the converted value when called.
Conversion formulas for your reference:
Metric Conversions Kilometerstomiles
Metric Conversions Milestokilometers
Hint: Modules are created as Python files with the same py file extension as your main program file. Your modules must be within the same folderdirectory as your program file in order to invoke functions from them.
Reference: Refer back to Learn to Program With Python : A StepbyStep Guide to Programming, nd edition, Chapter : Definition of a Function; and Programming in Python Chapter : Python Modules Module Definition Creating a Module.
Next you will create your Python program to accept user input of a distance value and unit of length miles or kilometers You will start by including the import statements at the top of this program. You will import both the miles and kilometers modules you just created.
Reminder: These three Python files must exist within the same directory.
Reference: Refer to Programming in Python Chapter : Python Modules Importing Modules.
Initialize variables. You should create two flag variables and initialize them to a value of true: one for when an invalid value is entered and one for when the user chooses to stop the processing ie validValue, processing
Create a conditional loop that will continue the processing until the user chooses to exit.
Hint: Use the processing flag you just created in step as your test condition.
Reference: Refer back to Learn to Program With Python : A StepbyStep Guide to Programming, nd edition, Chapter : Loops The While Statement.
Accept a distance value and unit of length from the user and store these two values in variables. Use prompts such as:
Please enter a distance value:
What is the unit of length M miles, KM kilometers:
Hint: Preface your input statement with float to ensure the value they enter is stored as a decimal number for use in the conversion formulas.
Reference: Refer back to your Unit readings in Learn to Program With Python : A StepbyStep Guide to Programming, nd edition, Chapter : BuiltIn Functions Getting Input From the User.
Incorporate a decision structure to check the unit of length entered by the user and call the appropriate conversion function from your userdefined modules.
If the user entered M call the convertToKilometers function passing in the user entered distance. Store the result of the function call in a new variable convertedDistance
If the user entered KM call the convertToMiles function passing in the user entered distance. Store the result of the function call in a new variable convertedDistance
Use an else statement for any other values and set the validValue flag to false to indicate an invalid value was entered.
Hint: Use the string function, upper, to convert the user input to uppercase and check for a value of M or KM only.
Reference: Refer back to Learn to Program With Python : A StepbyStep Guide to Programming, nd edition, Chapter : The if Statement Comparison Operators The elif Statement.
If the user entered a valid unit of length, display the distance they entered, the unit of measurement and the converted distanceunit Otherwise, display an error message. ie Your distance of miles is equivalent to kilometers or You entered an invalid unit of length.
Hint: Use an IFELSE decision structure here. Concatenate the results into one string.
Reference: Refer back to your Unit readings in Learn to Program With Python : A StepbyStep Guide to Programming, nd edition, Chapter : Print Statements; Chapter : BuiltIn Functions Concatenation; Chapter : The if Statement.
Add another prompt to ask the user if they would like to continue. ie Press X to quit or enter to continue processing.
If the user entered a value to discontinue processing, set the processing flag to false to conclude the conditional looping and end the program.
Note: These small programming examples include very minimal data validation. Feel free to expand upon this and add additional layers of validation.
Example Output
Please enter a distance value:
What is the unit of length M miles, KM kilometers : m
Your distance of miles is equivalent to kilometers
Press X to quit or enter to continue processing.
Please enter a distance value:
What is the unit of length M miles, KM kilometers : km
Your distance of
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
