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...

  • 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

  • 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 which takes a string argument and a character argument. It should return a...

    write a function which takes a string argument and a character argument. It should return a truth value (int 0 or 1), 0 if the string does not contain the character, and 1 if the string does contain the character. Do not use a built-in library for this. Again, call this function and its variables whatever you deem appropriate. The main function of the program should accept a single character followed by Enter. Then, it will read lines until the...

  • Write a python program that does the following: takes as input from the user an English...

    Write a python program that does the following: takes as input from the user an English sentence calls the function vc_counter() that: takes a string argument counts the number of vowels and consonants in the string returns a dictionary of the counts, using the keys total_vowels and total_consonants Uses the return from vc_counter() to print the total vowels and consonants with appropriate descriptions. Example: Enter an English sentence: The quick brown fox's jump landed past the lazy dog! Total #...

  • Write a python function called "get_sma", that takes two input arguments, similar to the example provided....

    Write a python function called "get_sma", that takes two input arguments, similar to the example provided. First argument is the original prices of type list, and the 2nd input argument being the number of days we want to calculate SMA, like a 8 day sma or 35 day sma. This function should return a new list of sma price, based on the input day range, and should be able to handle any SMA calculation, like: sma200 for 200 days moving...

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
ADVERTISEMENT