如果条件声明

时间:2020-07-22 15:44:17

标签: python python-3.x if-statement conditional-statements abstract-data-type

是否有任何方法可以使以下if语句更加优雅,而无需重复“ and”条件?抱歉,我是新手。

a = ' 10 20 30 40 50  '
print(a)
if '10' and '20' and '30' and '40' and '50' not in a :
    print('something is wrong')
else:
    print('This is correct')

谢谢

1 个答案:

答案 0 :(得分:0)

尝试此尺寸。应该检查所有在a_string中的匹配字符串,如果不是,则打印“出问题了”。

a_string = "10 20 30 40 50"
matches = ["10", "20", "30", "40", "50"]

if not all(x in a_string for x in matches):
   print('something is wrong')
else:
   print('This is correct')
相关问题