I am new to this site, but wanted to see if anyone could help me here
I am a newbie when it comes to programming, and I am currently trying to make a miles to km / km to miles program, which I based on a tutorial video I watched that taught functions.
I've written this short program successfully in which you can type in a specific amount of miles to convert to kilometer:
def miles_to_km(miles):
amount = miles / (1/1.609344)
print(miles, "miles are equal to", format(amount, '.2f'), "kilometers" )
miles = float(input("Please type in the amount of miles you would like to convert"))
miles_to_km(miles)
but I want to give users the choice of converting from miles to km or the other way around. This is my code now. Sorry if it's unorganized, I'm am a newbie
def miles_to_km():
miles = float(input("Please type in the amount of miles you would like to convert"))
amount = miles / (1/1.609344)
print(miles, "miles are equal to", format(amount, '.2f'), "kilometers")
def km_to_miles():
km = float(input("Please type in the amount of kilometers you would like to convert"))
amount = km * (1/1.609344)
print(km, "kilometers are equal to", format(amount, '.2f'), "kilometers")
print("Hello, would you like me to convert values from miles to kilometers or from kilometers to miles?")
question = float(input("Please press 1 for miles to km conversion, otherwise press 2 for km to miles conversion"))
if question is 1:
miles_to_km()
elif question is 2:
km_to_miles()
I don't get errors when running the program, but it finishes after asking the user to press 1 or 2 to choose which way they want to convert, I need help with running either miles_to_km or km_to_miles based on user input.
Hope somebody can help me, it is probably not difficult, but I couldn't figure it out.
miles / (1/1.609344)&km * (1/1.609344)?miles * 1.609344orkm/1.609344would make more sense.