-2

我收到,第一次迭代 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!')
4

1 回答 1

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))
于 2013-09-29T06:42:34.183 回答