为什么这种结构不起作用?

时间:2013-09-29 06:31:54

标签: python-3.x

我收到,第一次迭代1不是数字。

 numbers = ['1', 'apple', '2', '3', '4', '5']

 print ('Your numbers are...')
 for f in numbers:
     if f.isalpha():
         print ('This is not a number!') # (It actually isn't.)
         break
     print (f)
 else:
     print ('Here are your numbers!')

1 个答案:

答案 0 :(得分:1)

你看到了......

Your numbers are...

然后您点击第一次次迭代,f = '1'print (f)

1

然后你进入第二次迭代,f = 'apple'print ('This is not a number!') ......

This is not a number!

这是可以预期的。

使用此程序,您的输出会更清晰:

#!/usr/bin/env python3


numbers = ['1', 'apple', '2', '3', '4', '5']

print ('Your numbers are...')
for f in numbers:
    if f.isalpha():
        print('{} is not a number!'.format(f))
        break
else:
    print('Here are your numbers: {}'.format(numbers))