[英]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.我正在尝试制作一个简单的程序,该程序使用 for 循环、if-elif-else 语句和输入来获取三个“员工”的薪水并比较它们以查看 A,他们中的任何一个是否具有相同的薪水。 Or B if non of them have the same salary.或 B,如果他们没有相同的薪水。 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..}所以我决定根据您的要求重新编码所有内容{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.好的,所以.. 我创建了一个变量max_len = 3
,它是员工总数,然后我将变量COUNTER
初始化为 1,以便它在 while 循环中迭代并每次递增 1。 Therafter a variable named ask_sal
asks the user for salaries {3 times} and appends each input to the LIST_SAL
variable {a list}.然后一个名为ask_sal
的变量向用户询问薪水 {3 次},并将每个输入附加到LIST_SAL
变量 {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"..在我通过一个带有变量check
的for loop
访问列表 { list_sal
} 中的元素之后,该变量检查在 count() function 的帮助下查找元素在列表中重复的次数,其中,如果大于一个,则打印“提供相同的薪水”或打印“唯一薪水”..
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.您可能想知道为什么我使用了 2 个 break 语句。它们用于中断进一步的迭代,因为在第一次迭代中满足if or else
条件。 If you try removing the break statements, the print function will work the number of times the same salaries occur in the list.如果您尝试删除 break 语句,打印 function 将在列表中出现相同薪水的次数。
Hope I helped you.. I am too, a newbie in programming yet I love it and practice a lot.希望我对你有所帮助.. 我也是编程新手,但我喜欢它并且经常练习。 Keep grinding and Working hard继续打磨,努力工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.