单位转换器,用户可选择公里或英里

时间:2018-05-16 14:59:30

标签: python-2.7 converter

print "Hello! This is a unit converter between km and miles."
km = " "
miles = " "
if km:
    print "Please enter a number of kilometers that you'd like " \
          "to convert into miles. Enter only a number!"
    km = float(raw_input("Kilometers: "))
    km = miles * 1.609
    print "{0} kilometers is {1} miles.".format(km, miles)

else:
    print "Please enter a number of miles that you'd like " \
          "to convert into kilometers. Enter only a number!"
    miles = float(raw_input("Miles: "))
    miles = km * 0.621
    print "{0} miles is {1} kilometers.".format(miles,km)

我正在制作简单的单位转换器,我希望用户可以选择要转换的单位。我知道如何进行转换,但我遇到了一个问题,因为我不知道如何放置代码,因此用户可以选择英里或公里。 THX

1 个答案:

答案 0 :(得分:0)

我认为您希望从用户那里获得input,可能是这样的:

response = input('Enter km or miles: ')

if response == 'km':
    # convert km to miles
else:
    # convert miles to km