简体   繁体   中英

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.')

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")

I have tried running your code and it gives a horrible error. I clearly understood your desire. Most of your logic don't meet it. So I decided to RE-Code everything based upon your requirements {for loops, if, elif, else statement, 3 employees etc..}

MAX_LEN = 3
COUNTER = 1 
LIST_SAL = []
while MAX_LEN >= COUNTER:
    ASK_SAL = int(input('Enter your salary :'))
    LIST_SAL.append(ASK_SAL)
    COUNTER+=1
print(LIST_SAL)
for check in LIST_SAL:
    if LIST_SAL.count(check)>1:
        print('Same salaries presented in the list')
        break
    else:
        print('Salaries of employees are unique!')
        break 

Okay so.. I created a variable max_len = 3 which is the total number of employees and then I initiated the variable COUNTER to 1 so that it iterates in the while loop and gets incremented by 1 each time. Therafter a variable named ask_sal asks the user for salaries {3 times} and appends each input to the LIST_SAL variable {a list}. And then I printed the list for visualisation.

There after I accessed the elements in the list { list_sal } by a for loop with the variable check which looks for the number of time an element repeats in the list all by the help of count() function Which, IF greater then one prints "SAME salaries presented" OR else prints "Unique salary"..

You might be wondering why have I used 2 break statements.. Well they are used to break the further iteration as the if or else condition are met in the first iteration. If you try removing the break statements, the print function will work the number of times the same salaries occur in the list.

Hope I helped you.. I am too, a newbie in programming yet I love it and practice a lot. Keep grinding and Working hard

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM