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.
10 Antworten
+ 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.
+ 3
integer != integer
Is impossible as
3 != 3
Do you know lists?
It would help.
+ 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")
+ 1
I tried list but didn't know how to use them in this case
+ 1
It didn't work
+ 1
can you write for me to see?
+ 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.
0
That number need to have diferent variable name
0
thank you I figured out
- 1
U didn't declare i