What is wrong in below code? [duplicate]
Clash Royale CLAN TAG#URR8PPP
What is wrong in below code? [duplicate]
This question already has an answer here:
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
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
''
elif each == len(a):''
Why do you say something is wrong? Do not answer in comments, but in the question, using the edit feature.
– kabanus
Aug 10 at 6:46
why did you initialise each=1 ? What is the use in that?
– Mohamed Thasin ah
Aug 10 at 6:47
you can just check
if int(s) in a:
to see if it is in the list.– Bernhard
Aug 10 at 6:48
if int(s) in a:
@Bernhard it worked thanks :)
– Rachana
Aug 10 at 6:54
1 Answer
1
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"
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 at 6:59
raw_input()
a
What are the
''
inelif each == len(a):''
?– Joe
Aug 10 at 6:46