【问题标题】:How to weed out inputs that aren't in a list [duplicate]如何清除不在列表中的输入[重复]
【发布时间】:2017-10-05 18:53:29
【问题描述】:

我有一个代码,有人输入了元素周期表中前五个元素之一,我需要一种方法来判断输入是否不在列表中。到目前为止我有这个但它每次都说它是错误的(工作是输入变量):

if (work is not 'H','He','Li','Be',"B"):
    print ("That is not in the list of elements. Try again.")

【问题讨论】:

    标签: python list


    【解决方案1】:

    假设输入变量work 是正确的(没有多余的空格等),这就是我们使用in 运算符的方式:

    if work not in ('H', 'He', 'Li', 'Be', 'B'):
        print('That is not in the list of elements. Try again.')
    

    【讨论】:

      猜你喜欢
      • 2016-12-23
      • 2014-08-04
      • 2017-04-17
      • 1970-01-01
      • 2015-04-18
      • 1970-01-01
      • 2012-02-19
      • 2020-04-26
      • 2023-03-03
      相关资源
      最近更新 更多