[英]how can I exit program If there is no number in list(python)
我有一個清單
import sys
x = [[1,2], [3,4]]
這里發生了一些事情,x 的列表像這樣改變了。
x = [['A', 'B'], ['B', 'A']]
如果所有列表都填寫'A'或'B',結果應該是這樣的
print('there is no number in the list')
exit()
那么如果此列表中沒有數字,我該如何退出程序(python)
你可以這樣做:
try:
if all(value.isalpha() for lists in x for value in lists):
print('there is no number in the list')
exit()
except AttributeError:
pass
這個塊:
try:
<code>
except AttributeError:
pass
如果列表中的值不是字符串,將允許避免。
如果字符串中的所有字符都是字母並且至少有一個字符,則返回 True,否則返回 False。 字母字符是在Unicode字符數據庫中定義為“字母”的字符,即一般類別屬性為“Lm”、“Lt”、“Lu”、“Ll”或“Lo”之一的字符。 請注意,這與 Unicode 標准中定義的“字母”屬性不同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.