预计python中有两个空行pep8警告
我使用 vim 编辑器作为 python IDE.下面是一个简单的python程序来计算一个数的平方根:
I'm using vim editor as python IDE. Below is a simple python program to calculate square root of a number:
import cmath
def sqrt():
try:
num = int(input("Enter the number : "))
if num >= 0:
main(num)
else:
complex(num)
except:
print("OOPS..!!Something went wrong, try again")
sqrt()
return
def main(num):
squareRoot = num**(1/2)
print("The square Root of ", num, " is ", squareRoot)
return
def complex(num):
ans = cmath.sqrt(num)
print("The Square root if ", num, " is ", ans)
return
sqrt()
警告是:
1-square-root.py|2 col 1 C| E302 expected 2 blank lines, found 0 [pep8]
1-square-root.py|15 col 1 C| E302 expected 2 blank lines, found 1 [pep8]
1-square-root.py|21 col 1 C| E302 expected 2 blank lines, found 0 [pep8]
你能说出为什么会出现这些警告吗?
Can you please tell why these warnings are coming?
import cmath
def sqrt():
try:
num = int(input("Enter the number : "))
if num >= 0:
main(num)
else:
complex_num(num)
except:
print("OOPS..!!Something went wrong, try again")
sqrt()
return
def main(num):
square_root = num**(1/2)
print("The square Root of ", num, " is ", square_root)
return
def complex_num(num):
ans = cmath.sqrt(num)
print("The Square root if ", num, " is ", ans)
return
sqrt()
上一个将解决您的 PEP8 问题.导入后,您需要在开始代码之前添加 2 行新行.此外,在每个 def foo()
之间,您还需要有 2 个.
The previous will fix your PEP8 problems. After your import you need to have 2 new lines before starting your code. Also, between each def foo()
you need to have 2 as well.
在您的情况下,导入后有 0,并且每个函数之间有 1 个换行符.PEP8 的一部分,您需要在代码结束后换行.不幸的是,当我在这里粘贴您的代码时,我不知道如何显示它.
In your case you had 0 after import, and you had 1 newline between each function. Part of PEP8 you need to have a newline after the end of your code. Unfortunately I don't know how to show it when I paste your code in here.
注意命名,它也是 PEP8 的一部分.我将 complex
更改为 complex_num
以防止与内置 混淆复杂
.
Pay attention to the naming, it's part of PEP8 as well. I changed complex
to complex_num
to prevent confusion with builtin complex
.
最后,它们只是警告,如果需要,它们可以被忽略.
In the end, they're only warning, they can be ignored if needed.