我正在尝试制作一个简单的出租车票价估算器代码

时间:2019-10-10 02:46:16

标签: python

#Computes and outputs travel time
#For a given speed and distance
#Written by Emele Lasaqa 10/10/19

location = input("Enter your location in longitude and latitude: ")
location= int(location)
distance = input("Enter your distance in miles: ")
distance = float(distance)

time = distance/speed

print("At", location, "location latitude location longitude, it will take")
print(time, "hours to travel", distance, "miles.")

input("\n\nPress the Enter ket to exit"

我正在构建一个代码,计算从一个位置到另一个位置的出租车费, 所需的距离。但是我一直收到这个错误!有人可以帮我吗?

**This is the OUTPUT**

Enter your location in longitude and latitude: 18.1489° S, 178.4474° E

Traceback (most recent call last):

File "C:/Users/acer/Desktop/Distance - location calculator.py", line 5, in <module> 

location = input("Enter your location in longitude and latitude: ") 

File "<string>", line 1 

18.1489° S, 178.4474° E 
   ^ 
SyntaxError: invalid syntax

>>> 

1 个答案:

答案 0 :(得分:1)

您的位置是一个字符串,因此无法将其转换为int。您似乎还是不使用它。您也不提供速度。 如果是这样,那就更有意义了:

speed = input("Enter your speed: ")
speed = int(speed)
distance = input("Enter your distance in miles: ")
distance = float(distance)

time = distance / speed # works in python 3, if not cast as float
print(f"At {speed} it will take")
print(f"{time} hours to travel {distance} miles.")

您的脚本中有一些错误:

  • 未定义的变量
  • 尝试将字符串解析为整数(建议用户输入纬度,然后输入经度,然后将其转换为浮点数)
  • print函数仅接受一个参数,python 3中的首选方法是格式化字符串。