我如何根据约定在表达式和语句中使用空格?

问题描述 投票:0回答:2

我尝试提交此代码:

temperature = input("enter a tempereture as you wish: ")
convertion = int(temperature[:-1])
if temperature[-1] == "C":
    convertion = int((9 * convertion) / 5 + 32)
    temperature = str(convertion) + "F"
elif temperature[-1] == "F":
    convertion = int((5 * convertion) / 9 - 160/9)
    temperature = str(convertion) + "C"
print(temperature)

[这样做的时候,我被告知我的写作没有遵循惯例。我读了https://www.python.org/dev/peps/pep-0008/#whitespace-in-expressions-and-statements,但仍然无法理解我的错误。我在哪里错了?

python coding-style conventions
2个回答
1
投票

似乎在此行的结尾处:

convertion = int((5 * convertion) / 9 - 160/9)

您可以尝试:

convertion = int((5 * convertion) / 9 - 160 / 9)

0
投票
temperature = input("enter a temperature as you wish: ")

conversion = int(temperature[:-1])

if temperature[-1] == "C":

    conversion = int((9 * conversion) / 5 + 32)

    temperature = str(conversion) + "F"

elif temperature[-1] == "F":

    conversion = int((5 * conversion) / 9 - 160 / 9)

    temperature = str(conversion) + "C"

print(temperature)

您可以尝试使用上面的代码吗?虽然没有太大变化!

© www.soinside.com 2019 - 2024. All rights reserved.