[英]Searching a list and displaying whether or not the list contains the search variable
我有一個Twitter管理器程序,其中一個選項是搜索包含用戶提供的任何輸入的推文。 當列表中找到匹配項時,我無法找出讓for循環跳過else語句的方法。 現在開始運行,程序將打印找到的符合搜索條件的推文,但還會打印未找到包含搜索條件的推文。
# Option 3, search tweet_list for specific content and display if found
# in descending order starting with the most recent.
elif count == 3:
tweet_list.reverse()
if len(tweet_list) == 0:
print('There are no tweets to search.', '\n')
else:
search = input('What would you like to search for? ')
print('\nSearch Results')
print('--------------')
for n in tweet_list:
a = n.get_author()
b = n.get_text()
c = n.get_age()
if search in a or search in b:
print(a + '-' + c + '\n' + b + '\n')
else:
print('No tweets contain "' + search + '".' + '\n')
print('')
創建一個說found
的變量,並將其初始化為False
。 找到推文后,將其設置為True
。 然后將else:
替換為if not found:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.