我可以将 for 循环集成到检查数组中是否存在数字的 if 语句中吗?

Can I integrate a for loop into a if statement that checks if a number exists in an array?

我正在编写一个简单的代码,对数组数字执行线性搜索,以查看其中是否存在用户输入的数字,但我不断收到语法错误。


numbers =[1,2,3,4,5,6,7,8,9]
num= Input("enter number")
If num[for count in range(0,len(numbers))] ==numbers[for count in range(0,len(numbers))]:
    print("num is found")
else:
    print("num not found")

我是否正确地写了 IF 语句,还是我写错了

试试这个:

numbers =[1,2,3,4,5,6,7,8,9]
num= input("enter number")
if int(num) in numbers:
    print("num is found")
else:
    print("num not found")

另外,如果你想让它解释非数字响应,你可以使用:

numbers =[1,2,3,4,5,6,7,8,9]
num= input("enter number")
try:
    if int(num) in numbers:
        print("num is found")
    else:
        print("num not found")
except:
    print("Sorry, you can only enter integers.")