if a is not an element in a list how to print?

This question already has an answer here:

  • Finding the index of an item given a list containing it in Python 23 answers

  • I think your looking something like the below example

    my_list = [1, 3, 5]
    number = input("Choose a number from 1 to 5: ")
    if int(number) in my_list:
        print("error. . . . ")
    else:
        print(int(number))
    

    And when you run it this is how it works

    Choose a number from 1 to 5: 2

    2

    Choose a number from 1 to 5: 1

    error. . . .


    首先,您需要将用户输入视为int ,然后检查用户输入的数字是否存在于my_list

    my_list = [1, 3, 5]
    number = int(input("Choose a number from 1 to 5: "))
    
    
    if number in my_list:
      print('Error')
    else:
      print(number)
    
    链接地址: http://www.djcxy.com/p/28128.html

    上一篇: 你如何从列表中检索单词的位置?

    下一篇: 如果a不是列表中的元素如何打印?