-2

The assignment:

Write a program that will list at least 6 numbers for the customer. The program must process the received list and write down on each line whether the current number has been repeated before. If repeated, write "YES", if not - "NO". for example:

Step 1: The user enters at least 6 numbers that are stored in the array, say included [1, 2, 1, 1, 4, 2]

Step 2: The program will process the received list and display the answers on the following screen:

NO 
NO 
YES 
YES 
NO 
YES

My code:

x = []
y = []
for i in range (6):
   i = int(input("Enter a number: "))
   x.append(i)

for z in range(len(x)):
     for j in range (z + 1,len(x)):
        if x[z] == x[j]:
            y.append("YES")
        else:
             y.append("NO")         
        if "YES" in y:
           print("YES")
           y.clear()
        else:
           print("NO")
           y.clear()

i created y for just an experiment dont use it if you want to. all i want is to check if one element is the same as one of the other elements and if so give me only one YES not NO NO NO YES NO or something like that and if their are not the same just one NO

Barmar
  • 741,623
  • 53
  • 500
  • 612
IG-_-
  • 13
  • 2
  • So you just want to know if there are any duplicates in `x`? – Barmar Mar 16 '21 at 18:58
  • the task was a user has to enter 6 numbers and if any of those 6 numbers match it has to say YES and if they dont it has to say NO. for example: i type in 2, 3, 4, 5, 2, 4 it will say NO, NO, NO, NO, YES, YES – IG-_- Mar 16 '21 at 19:00
  • Isn't that the same as testing if there are any duplicates in the list? – Barmar Mar 16 '21 at 19:02
  • yeah i guess so but it gives me answer for every comparison i just want one answer for one loop – IG-_- Mar 16 '21 at 19:05
  • Write a program that will list at least 6 numbers for the customer. The program must process the received list and write down on each line whether the current number has been repeated before. If repeated, write "YES", if not - "NO". for example: Step 1: The user enters at least 6 numbers that are stored in the array, say included [1, 2, 1, 1, 4, 2] Step 2: The program will process the received list and display the answers on the following screen: NO NO YES YES NO YES this is the task i was given – IG-_- Mar 16 '21 at 19:06
  • So you want 6 answers, not just 1. – Barmar Mar 16 '21 at 19:07
  • Put the assignment in the question. – Barmar Mar 16 '21 at 19:09

1 Answers1

1

Use a slice to check whether each element is in the preceding part of the list. You can use the built-in in operator to test whether there's a duplicate, rather than using a nested loop.

for i, num in enumerate(x):
    if num in x[:i]:
        print("YES")
    else:
        print("NO")
Barmar
  • 741,623
  • 53
  • 500
  • 612