Python Forum
Validate a List and print YES or NO
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Validate a List and print YES or NO
#1
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)
Reply
#2
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).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Why do you start with A not being empty? And what have you tried to detect 1, 2, and 3?
Reply
#4
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)
Reply
#5
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.
Reply
#6
(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")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do you get Python to print just one value in a list? 357mag 3 1,038 May-17-2023, 09:52 PM
Last Post: rob101
  validate large json file with millions of records in batches herobpv 3 1,299 Dec-10-2022, 10:36 PM
Last Post: bowlofred
  Create SQL connection function and validate mg24 1 962 Sep-30-2022, 07:45 PM
Last Post: deanhystad
Sad how to validate user input from database johnconar 3 1,948 Sep-11-2022, 12:36 PM
Last Post: ndc85430
  Print List to Terminal DaveG 2 1,447 Apr-02-2022, 11:25 AM
Last Post: perfringo
  Print max numbers in a list jimmoriarty 1 2,172 Sep-25-2020, 07:29 AM
Last Post: DPaul
  Print variable values from a list of variables xnightwingx 3 2,656 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  Unable to Validate csv blanck data and write in csv prashant18 0 1,547 Jul-25-2020, 12:08 PM
Last Post: prashant18
  Print the number of items in a list on ubuntu terminal buttercup 2 1,958 Jul-24-2020, 01:46 PM
Last Post: ndc85430
  taking input doesnt print as list bntayfur 2 2,142 Jun-04-2020, 02:48 AM
Last Post: bntayfur

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020