Question

#function to covert kilometer to miles #takes km input as an argument and return the calculation...

#function to covert kilometer to miles
#takes km input as an argument and return the calculation
def kilometer_to_miles(km):
return km * 0.6214
#function to covert miles to kilometer
#takes miles input as an argument and return the calculation
def miles_to_kilometer(miles):
return miles/0.6214
#Main function to find the distance
#by calling either of the two functions
#prompt the user to enter his/her choice of distance conversion
restart = 0
while (restart == 'y'):
user_input = input ("Enter k to convert kilometer to miles"+ \
"Enter m to converts miles to kilometer: ")
#if statement to execute the user choice of conversion
#Calls the kilometer_to_miles function
if user_input.lower() == 'k':
km = float(input("Enter kilometer to convert to mile: "))
print (km, "makes" ,format(kilometer_to_miles(km),'.2f') , "miles")
#calls the miles_to_kilometer function
elif user_input.lower() == 'm':
miles = float(input("Enter miles to convert to kilometer: "))
print (miles, "miles makes" ,format(miles_to_kilometer(miles),'.2f')
,"kilometer")
#execute if the user's inpupt is none of the above
else:
print("invalid input.")
restart = input ("Enter Y to enter another value and N to quit.")

The problem is it is first printing the restart statement before doing the block under while loop.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is code:

#function to covert kilometer to miles

#takes km input as an argument and return the calculation

def kilometer_to_miles(km):

return km * 0.6214

#function to covert miles to kilometer

#takes miles input as an argument and return the calculation

def miles_to_kilometer(miles):

return miles/0.6214

#Main function to find the distance

#by calling either of the two functions

#prompt the user to enter his/her choice of distance conversion

restart = 'y'

while (restart == 'y'):

user_input = input ("Enter k to convert kilometer to miles"+ \

"Enter m to converts miles to kilometer: ")

#if statement to execute the user choice of conversion

#Calls the kilometer_to_miles function

if user_input.lower() == 'k':

km = float(input("Enter kilometer to convert to mile: "))

print (km, "makes" ,format(kilometer_to_miles(km),'.2f') , "miles")

#calls the miles_to_kilometer function

elif user_input.lower() == 'm':

miles = float(input("Enter miles to convert to kilometer: "))

print (miles, "miles makes" ,format(miles_to_kilometer(miles),'.2f'),"kilometer")

#execute if the user's inpupt is none of the above

else:

print("invalid input.")

restart = input ("Enter Y to enter another value and N to quit.")

Output:

Add a comment
Know the answer?
Add Answer to:
#function to covert kilometer to miles #takes km input as an argument and return the calculation...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For this portion of the lab, you will reuse the program you wrote before That means...

    For this portion of the lab, you will reuse the program you wrote before That means you will open the lab you wrote in the previous assignment and change it. You should NOT start from scratch. Also, all the requirements/prohibitions from the previous lab MUST also be included /omitted from this lab. Redesign the solution in the following manner: 1. All the functions used are value-returning functions. 2. Put the functions in an external module and let the main program...

  • I need to write a function that takes in 3 parameters, parameter_1, parameter_2, and parameter_3. It computes a value ba...

    I need to write a function that takes in 3 parameters, parameter_1, parameter_2, and parameter_3. It computes a value based upon the following formula and places it in the return_var variable: return_var = (parameter_1 + parameter_2 * 47)/parameter_3 The value maintained in return_var variable must be returned by the function. def main(): argA int (input("Enter value for Argument A: ")) argB int(input("Enter value for Argument B ")) argC int (input ("Enter value for Argument C ")) #invoke the crazy_formula function...

  • I need to write a function that takes in 3 parameters, parameter_1, parameter_2, and parameter_3. It computes a value ba...

    I need to write a function that takes in 3 parameters, parameter_1, parameter_2, and parameter_3. It computes a value based upon the following formula and places it in the return_var variable: return_var = (parameter_1 + parameter_2 * 47)/parameter_3 The value maintained in return_var variable must be returned by the function. def main(): argA int (input("Enter value for Argument A: ")) argB int(input("Enter value for Argument B ")) argC int (input ("Enter value for Argument C ")) #invoke the crazy_formula function...

  • The problem is this Design a function that accepts a list as an argument and returns...

    The problem is this Design a function that accepts a list as an argument and returns the largest value in the list. The function should use recursion to find the largest item. My coding so far is this def main(): # Prints the largest value in list user_input = input('Enter a list of numbers seperated by a space: ') user_list = user_input.split() print('Largest number is ', max_Number(user_list), '.') # Recursion function that finds the maximum number in a sequence of...

  • Define a function called “mtok” that takes one input parameter representing a distance in miles and...

    Define a function called “mtok” that takes one input parameter representing a distance in miles and prints out the distance in Kilometers. Assume 1 mile equals 1.5 Km. Example Function Calls: mtok(2) # should print: 2 mile(s) equals 3.0 km mtok(10) # should print: 10 mile(s) equals 15 km

  • GUI Programming Creating a Kilometer to Miles Converter Lab Assignment Objectives Understand the basics of tkinter...

    GUI Programming Creating a Kilometer to Miles Converter Lab Assignment Objectives Understand the basics of tkinter GUI development. Based on an informal application specification be able to develop a tkinter GUI program that contains one or more Label widgets. Be able to prompt user for input to a GUI application using a messagebox. Obtain user input into a GUI application that can be used for event driven selection. Be able to develop a tkinter GUI program that supports event-based widgets....

  • Calculate the Balance - Withdrawal If the action is Withdrawal ‘W’, use the withdrawal function to...

    Calculate the Balance - Withdrawal If the action is Withdrawal ‘W’, use the withdrawal function to remove funds from the account Hint – The formatter for a float value in Python is %f. With the formatter %.2f, you format the float to have 2 decimal places. For example: print("Account balance: $%.2f" % account_balance) Withdrawal Information Withdraw Input userchoice = input ("What would you like to do?\n") userchoice = 'W' withdrawal_amount = 100 Withdraw Output What would you like to do?...

  • 6.5.1: Functions: Factoring out a unit-conversion calculation. PYTHON CODING Write a function so that the main program b...

    6.5.1: Functions: Factoring out a unit-conversion calculation.PYTHON CODINGWrite a function so that the main program below can be replaced by the simpler code that calls function mph_and_minutes_to_miles(). Original main program:miles_per_hour = float(input()) minutes_traveled = float(input()) hours_traveled = minutes_traveled / 60.0 miles_traveled = hours_traveled * miles_per_hour print('Miles: %f' % miles_traveled)Sample output with inputs: 70.0 100.0Miles: 116.666667

  • use this code of converting Km to miles , to create Temperature converter by using java...

    use this code of converting Km to miles , to create Temperature converter by using java FX import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.geometry.Pos; import javafx.geometry.Insets; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.control.Button; import javafx.event.EventHandler; import javafx.event.ActionEvent; /** * Kilometer Converter application */ public class KiloConverter extends Application { // Fields private TextField kiloTextField; private Label resultLabel; public static void main(String[] args) { // Launch the application. launch(args); } @Override public void start(Stage primaryStage) { //...

  • Write a function that takes, as an argument, the name of a file, fileName . Your...

    Write a function that takes, as an argument, the name of a file, fileName . Your program should open (and read through) the file specified, and return the maximum value in the file. Name this function maxValueInFile(fileName). def openFile(): # Prompt for the file name filename =input("Enter a file name: " ) # Open the file for reading file = open(filename) # Prompt for the target value target =input("Enter a threshold value: ") # Initialize the counter counter = 0...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT