我不知道我的代码中的错误在哪里

2024-06-13 06:37:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python新手,找不到错误所在。Python说有语法错误,但它不会告诉我它在哪里。我的任务是制作一个计程车计票程序。这是我的密码

distance = input("Enter the distance of the taxi ride in kilometers (km): ")
passengers = input("Enter the amount of passengers from 1 to 5: ")

cost = float(distance) + 3 - 1 * 2

if passengers == 5:
    cost = cost * 2
    print("The total cost of the journey is £" cost) 
else:
    print("The total cost of the journey is £" cost) 

这个错误可能很简单,但我找不到。提前谢谢


Tags: oftheinputis错误distancetotalprint
2条回答

如果您使用的是python 3.6或更高版本,则不能连接字符串,请使用f字符串:

distance = input("Enter the distance of the taxi ride in kilometers (km): ")
passengers = input("Enter the amount of passengers from 1 to 5: ")

cost = float(distance) + 3 - 1 * 2

if passengers == 5:
    cost = cost * 2
    print(f"The total cost of the journey is £ {cost}") 
else:
    print(f"The total cost of the journey is £ {cost}") 

如果要使用print()函数打印多个内容,则需要使用逗号将它们分隔开

试试这个:

print("The total cost of the journey is £", cost)

相关问题 更多 >