首页 > 解决方案 > 执行简单循环时Python中的意外输出

问题描述

我有这个相当简单的循环

cities = ['Merano', 'Madrid', 'New York', 'Bangkok']
countries = ['Italy', 'Spain', 'USA']

for index, city in enumerate(cities):
    print('This is index:', index)
    print('This is city:', city)
    print('The length of the list \'cities\' is: ', len(cities))
    print(print('The length of the list \'countries\' is: ', len(countries)))
    print(countries[index])
    print(5 * '#')

我希望它会破裂。然而,我没想到的是这个输出。

This is index: 0
This is city: Merano
The length of the list 'cities' is:  4
The length of the list 'countries' is:  3
None
Italy
#####
This is index: 1
This is city: Madrid
The length of the list 'cities' is:  4
The length of the list 'countries' is:  3
None
Spain
....

从哪里来none?我不print这样做,也不执行我希望打印的东西......

标签: pythonpython-3.x

解决方案


print(print('The length of the list \'countries\' is: ', len(countries))) 你在使用中使用了 print(print())

print('The length of the list \'countries\' is: ', len(countries))`

推荐阅读