-1

Program to find whether the given number is present in given list of numbers in python. What is wrong in below code? It never prints "number is present".

import sys

a = [1,2,3,4,5,6,7,8]
each = 1
s = raw_input("eneter a number ")

for each in range(0,len(a)):
    if s == a[each]:
        print "number is present"
        sys.exit()
    elif each == len(a):
        print "not present"
    else:
        continue
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149

1 Answers1

2

You can write your code in this way:

a = [1,2,3,4,5,6,7,8]
s = int(raw_input("enter a number "))
if s in a:
    print "number is present"
else:
    print "not present"
Joe
  • 12,057
  • 5
  • 39
  • 55
  • An additional hint explaining, that the string returned by `raw_input()` will never match the integers given in list `a` would complete this answer. – guidot Aug 10 '18 at 06:59