Python程序,从用户那里获取输入,将里程转换为公里

时间:2016-04-03 23:25:42

标签: python-3.x

这是我到目前为止的代码,当我运行它时出现语法错误,我不知道为什么

#input from the user 
def main()
miles = float(input("Enter a distance in miles: "))


conv_fac = 1.609 #conversion factor 

#calculating how many kilometers 
kilometers = miles * conv_fac


print("The distance in kilometers is: ", kilometers)

main()

2 个答案:

答案 0 :(得分:1)

您未在:功能后添加main()个符号 应该是

#input from the user 
def main():
    miles = float(input("Enter a distance in miles: "))
    conv_fac = 1.609 #conversion factor 
    #calculating how many kilometers 
    kilometers = miles * conv_fac
    print("The distance in kilometers is: ", kilometers)

main()

这是结果:

enter image description here

答案 1 :(得分:-1)

def convert_to_miles(miles):                                                         
    return miles * 1.6                                                         

print convert_to_miles(miles)