[英]Python IndentationError in executing my code
我只是在學習python。 我正在嘗試編寫這樣的簡單代碼。
def isPrime(n):
a=0
if n==1:
print("1 is special")
for x in range(1,n):
if n%x==0:
a=a+1
if a==2:
print("{} is prime".format(n))
else:
print("{} is not prime".format(n))
for n in range(2,20):
isPrime(n)
然后,我在第7行收到一個錯誤,指出“ IndentationError:期望縮進的塊”。實際上是什么錯誤? 有人可以幫忙嗎? 抱歉,這很愚蠢。 我對python完全陌生。
您的錯誤在於第二個if語句:
def isPrime(n):
a=0
if n==1:
print("n is special")
for x in range(1,n):
if n%x==0:
a=a+1 #this line should be indented as it currently is
#can also be written as: a+=1
if a==2:
print("{} is prime".format(n))
else:
print("{} is not prime".format(n))
for n in range(2,20):
isPrime(n)
if n%x==0:
a=a+1 <-- needs to be indented
錯誤是文字上的錯誤,當您看到錯誤時,請查看循環,if語句和函數以查看行進的行是否縮進。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.