我想弄清楚这段代码是如何工作的。 i
如何在 for 循环之外访问?
# Palindrome of string
str=raw_input("Enter the string\n")
ln=len(str)
for i in range(ln/2) :
if(str[ln-i-1]!=str[i]):
break
if(i==(ln/2)-1): ## How is i accessible outside the for loop ?
print "Palindrome"
else:
print "Not Palindrome"
最佳答案
这是 Python 的一部分。在 for 循环内声明的变量(包括循环计数器)在完全离开作用域之前不会衰减。
看看这个问题:
来自答案:
for foo in xrange(10): bar = 2 print(foo, bar)
The above will print (9,2).
关于python - Python 中的循环计数范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38380819/