Python Forum
Validate a List and print YES or NO - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Validate a List and print YES or NO (/thread-925.html)



Validate a List and print YES or NO - Aoleone# - Nov-15-2016

Hi Guys

Any help on this will be truly appreciated. I'm writing program which ask, first , for the length of a list, followed by the inputting the elements in the list. I want the program to print 'YES' if A contains numbers 1, 2, 3 (all of them)in an arbitrary order and "NO" otherwise. For instance, the program should print "YES" if A = [10, 2, 4, 15, 3, 6, 1] and "NO" if A = [1, 17, 2, 45] (because number 3 is missing). Below is what I've done so far.



n=int(input("Please enter the list length "))
A=[10,11,71,17,27,28]
for i in range (0,n):
  print("Entering element:", i)
  CurEl=int(input("Please enter the element"))
  A.append(CurEl)



RE: Validate a List and print YES or NO - ichabod801 - Nov-15-2016

You probably want to start with an empty list (A = []), so it only contains the elements entered by the user. There are a couple ways you could do the testing. The simplest is the in operator:

if 1 in A:
But you'll have to do that for all three numbers. You could do that in a loop or combine them with the and operator.

Note that range(0, n) is the same as range(n).


RE: Validate a List and print YES or NO - micseydel - Nov-15-2016

Why do you start with A not being empty? And what have you tried to detect 1, 2, and 3?


RE: Validate a List and print YES or NO - Aoleone# - Nov-15-2016

Hi Guys, I'll be grateful for a bit more illustration. Just started learning python. I tried below but not so good.
A=[]
N= input("Enter some numbers: ")
for i in range(n):
  D = int(input("Enter a single number"))
  A.append(D)
    print(C)



RE: Validate a List and print YES or NO - micseydel - Nov-15-2016

What do you intend for that last line to do? What have you tried for the second part of the assignment?

Make sure to use code tags in the future.


RE: Validate a List and print YES or NO - nilamo - Nov-16-2016

(Nov-15-2016, 02:21 AM)Aoleone# Wrote: Hi Guys, I'll be grateful for a bit more illustration. Just started learning python. I tried below but not so good.
A=[]
N= input("Enter some numbers: ")
for i in range(n):
  D = int(input("Enter a single number"))
  A.append(D)
    print(C)

Why all the single character variable names? Normally, single-character variables are a big flag to anyone reading your code that says "this value doesn't really matter, and it isn't used for anything important. Feel free to ignore." Wouldn't it make more sense to give them labels you can remember, like "items" or "length"?  Also, what's "C", and why is it indented?

N should be passed through int(), otherwise range() will fail since it can't give a range over a string.  You're doing a good job building the list, so next (AFTER the for loop) you should use something like an if statement to check if what you want is inside the list.

Unless you just want to copy/paste some code.  Then feel free to use this :)
print("YES" if set(range(1, 4)).issubset(int(input("Single number please: ")) for i in range(int(input("How many items? ")))) else "NO")