Can someone tell me whats wrog with this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can someone tell me whats wrog with this code?

Here's my code #This program's purpose is to tell if integers are distinct number = int(input("Enter a number")) for i in range(number): integer = int(input("Enter an integer :")) if integer != integer: print("The integers entered are distinct.") else: print("You entered repeated integers") can someone tell me why itsn't working? Even when I input different integers it still displays that I entered repeated integers.

25th Oct 2021, 2:41 PM
Rim Lamghari
Rim Lamghari - avatar
10 Answers
+ 4
Okay.... at the beginning numbers = [ ] Now append each input to the list. Now tatatata🥳🥳🥳 the trick how to find out if there are duplicates in the list: If len(set(numbers)) == len(numbers): #no duplicates Give it a try and ask again if u struggle.
25th Oct 2021, 5:59 PM
Oma Falk
Oma Falk - avatar
+ 3
integer != integer Is impossible as 3 != 3 Do you know lists? It would help.
25th Oct 2021, 2:47 PM
Oma Falk
Oma Falk - avatar
+ 3
nums=[] number = int(input("Enter a number")) for i in range(number): nums.append(int(input("Enter an integer :"))) if len(set(nums)) == number: print("The integers entered are distinct.") else: print("You entered repeated integers")
26th Oct 2021, 4:39 PM
Oma Falk
Oma Falk - avatar
+ 1
I tried list but didn't know how to use them in this case
25th Oct 2021, 5:10 PM
Rim Lamghari
Rim Lamghari - avatar
+ 1
It didn't work
25th Oct 2021, 7:17 PM
Rim Lamghari
Rim Lamghari - avatar
+ 1
can you write for me to see?
25th Oct 2021, 7:32 PM
Rim Lamghari
Rim Lamghari - avatar
+ 1
Using lists are definitely easier. You append all of the integers to a list. And then you run a loop through the list to check if the elements are distinct. This is a sample code: N=int(input("Enter a no:")) Lst=[] for i in range(N):   x=int(input("enter an integer:"))   Lst.append(x) for i in range(len(Lst)):     if Lst[i]==Lst[i-1]:        print("The integers are not distinct") else:     print("Distinct") Hope it helps.
27th Oct 2021, 6:59 AM
Rose Bobby
Rose Bobby - avatar
0
That number need to have diferent variable name
26th Oct 2021, 4:43 AM
Cristian Baeza Jimenez
Cristian Baeza Jimenez - avatar
0
thank you I figured out
27th Oct 2021, 8:17 PM
Rim Lamghari
Rim Lamghari - avatar
- 1
U didn't declare i
25th Oct 2021, 2:42 PM
Jolen Mascarenhas