Python 续行符(\)“拯救”你的超长语句
特别注意:续行符(\)只能在行尾,她后面什么都不能有,空格也不行,有则报错。要彻彻底底的“行尾”。👀👀
不使用续行符(\)代码
# coding: utf-8
'''加油量计算使用了海象运算符 := ,
不作修改请在 Python 3.9+ 环境运行,
否则报错。'''
milage_last, milage_local = 4257, 4481
k = '上次加满'
oil_price, sum_of_money = 7.73, 200
oil_wear = ((sum_oil := sum_of_money / oil_price) / (sum_milage := milage_local - milage_last)) * 100
print(f'\n\n\n{"油耗计算器":_^37}\n\n\n{"上次里程:":>15}{milage_last} km\n{"当前里程:":>15}{milage_local} km\n{"加油状态:":>15}{k}\n{"油耗里程:":>15}{sum_milage} km\n\n{"Cut-off Rule":.^42}\n\n{"当前油价:":>15}¥{oil_price} 元/升\n{"加油金额:":>15}¥{sum_of_money} 元\n{"加 油 量:":>16}{sum_oil:.2f} 升\n\n{"Cut-off Rule":.^42}\n\n\n{"当前油耗:":>15}{oil_wear:.2f} 升/100km\n\n{"":>19}¥ {oil_price * oil_wear / 100:.2f} 元/km\n\n\n{"The End":_^42}\n')
最后一行 print() ,语句超长,非常难读。😭
肿么办?🧐别慌!!!
续行符(\)前来救驾。 🤠
使用续行符(\)代码
# coding: utf-8
'''加油量计算使用了海象运算符 := ,
不作修改请在 Python 3.9+ 环境运行,
否则报错。'''
milage_last, milage_local = 4257, 4481
k = '上次加满'
oil_price, sum_of_money = 7.73, 200
oil_wear = ((sum_oil := sum_of_money / oil_price)\
/ (sum_milage := milage_local - milage_last)\
) * 100
print(f'\n\n\n{"油耗计算器":_^37}\
\n\n\n{"上次里程:":>15}{milage_last} km\
\n{"当前里程:":>15}{milage_local} km\
\n{"加油状态:":>15}{k}\
\n{"油耗里程:":>15}{sum_milage} km\n\
\n{"Cut-off Rule":.^42}\n\
\n{"当前油价:":>15}¥{oil_price} 元/升\
\n{"加油金额:":>15}¥{sum_of_money} 元\
\n{"加 油 量:":>16}{sum_oil:.2f} 升\n\
\n{"Cut-off Rule":.^42}\n\
\n\n{"当前油耗:":>15}{oil_wear:.2f} 升/100km\
\n\n{"":>19}¥ {oil_price * oil_wear / 100:.2f} 元/km\
\n\n\n{"The End":_^42}\n')
回首页
代码运行效果