Bod*_*rma 1 python syntax-error
我有一个trip_cost计算假期总费用的功能.如果我想打印函数的结果我可以这样做而没有问题:
print trip_cost(city, days, spending_money)
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试使用字符串编写更易于理解,用户友好的版本,我会得到一个 Syntax Error: Invalid Syntax
print "Your total trip cost is: " trip_cost(city, days, spending_money)
Run Code Online (Sandbox Code Playgroud)
怎样才能解决这个问题?
使用format()字符串方法:
print "Your total trip cost is: {}".format(trip_cost(city, days, spending_money))
Run Code Online (Sandbox Code Playgroud)
Python 3.6+的更新:
您可以在Python 3.6+中使用格式化的字符串文字
print(f"Your total trip cost is: {trip_cost(city, days, spending_money)}")
Run Code Online (Sandbox Code Playgroud)