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?

0

Issue

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

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

Answered By – LunaticXXD10

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More