Python 代码错误分析:for 循环与 while 循环中的缩进问题
这个程序的错误在于缩进问题,导致 if 语句的判断位置不正确。正确的程序应该如下:
for x in range(100, 1000):
y = 0
temp = x
while temp > 0:
g = temp % 10
y = y * 10 + g
temp = temp // 10
if x == y:
print(x, end='\t')
需要注意的是,在 while 循环中,不能直接修改 x 的值,否则会影响到后面的循环判断。因此需要使用一个临时变量 temp 来存储 x 的值,同时在 if 语句判断完之后再输出 x 的值。
原文地址: https://www.cveoy.top/t/topic/n5P2 著作权归作者所有。请勿转载和采集!