Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I take an input, add it to a list, and use loops and if elif else statements to check if any two values are the same?

I am trying to make a simple program that uses for loops, if-elif-else statements, and input to take three "employees" salaries and compare them to see if A, any of them have the same salary. Or B if non of them have the same salary. So far, this is what I got but both strategies I’ve tried haven’t been successful and I don’t understand why :(. All help is appreciated, and spare me for I’m still new to coding and I’m trying to teach myself through these exercises. Thank you kindly!

salarylist = [] 
salarylist = list(map(int, salarylist))


maxLengthList = 3
while len(salarylist) < maxLengthList:
    salary_of_employee = int(input("Enter your salary: "))
    salarylist.append(salary_of_employee)
print("salary of employees are:\n")
print(type(salarylist))
print(salarylist)
print(type(salarylist))
x = salarylist
if salary_of_employee == salarylist[x]:
    print(f"these are the same salary.{salary_of_employee} and {salarylist[x]}")
else:
    print('non of the salaries are the same.')

    
############################################################################   
    
    
empOne = salarylist[0]
empTwo = salarylist[1]
empThree = salarylist[2]

if empOne == salarylist[0]:
    print("these are the same salary.")
elif empTwo == salarylist[1]:
    print('these are the same salary')
elif empThree == salarylist[2]:
    print('these are the same salary')
else:
    print('non of the salaries are the same.')

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When you have the list of salaries, create a set from that list and compare the length, if same length then salaries will be unique, otherwise there will be at least one common salary


lst2 = set(salarylist)

if(len(lst2) == len(salarylist)):
  print("Non of the salaries are the same.")
else:
  print("Same salaries found")

To find the same salaries loop through the the set and check count of elements in the list, if greater than one, then duplicate.

for sal in lst2:
  if(salarylist.count(sal) > 1):
    print("Duplicate here")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading