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