使用Python,编写一个程序来检查员工在服务和办公期间的年数。用户将输入数年和在役和下列办公室(它,会计,人力资源)检查以下条件。办公室IT ACCT人力资源年超过或等于10年10000 12000 15000低于10年5000 6000 7500
我已经有我的密码了但我很难..。把它变得更具体
# take the input from user
year = int(input('Please enter the years: '))
office = input('Please enter the office name: ')
# if-else condition
if (office.upper() == "IT" or office.lower() == "it") or (office.upper() == "ACCT" or office.lower() == "acct") or (office.upper() == "HR" or office.lower() == "hr"):
{
print("Employee office is: "+ office)
}
else:
{
print("The wrong office name entered")
}
# if-else condition
if year >=10:
{
print("10000 12000 15000")
}
else:
{
print("5000 6000 7500")
}但是预期的结果应该是这样的:
进入服务年限:15年
进入办公室: IT
按年份/支出计算的薪金: 10000
发布于 2021-12-27 15:08:15
您只需要在单独的office名称if-语句中重复年份if-语句:
year = int(input('Please enter the years: '))
office = input('Please enter the office name: ')
print("\nsalary based on year/exp: ",end='')
if office.upper() == "IT" or office.lower() == "it":
if year >=10: print(10000)
else: print(5000)
elif office.upper() == "ACCT" or office.lower() == "acct":
if year >=10: print(12000)
else: print(6000)
elif office.upper() == "HR" or office.lower() == "hr":
if year >=10: print(15000)
else: print(7500)
else:
print("The wrong office name entered")发布于 2021-12-27 15:12:42
您可以使用字典来存储键和值对,在其中可以使用键访问值。因此,让关键是办公室的名称和价值元组包含的工资少于10年和以上或10年。然后询问办公室名称,并立即降低返回的值,以使事情变得更简单。然后请求服务年数,并将返回的值转换为整数,以便进行比较。然后从字典中获取值,如果键不存在(不正确的office名称),则返回两个值的元组,以便以后使用索引不会引发异常。然后根据年份在元组中打印第一个值或第二个值:
office_salary = {
'it': (5000, 10_000),
'acct': (6000, 12_000),
'hr': (7500, 15_000)
}
office = input('Enter office name: ').lower()
years = int(input('Enter years in service: '))
salaries = office_salary.get(office, (None, None))
if years < 10:
print(salaries[0])
elif years >= 10:
print(salaries[1]发布于 2021-12-27 15:22:05
如果我没听错的话:
# take the input from user
year = int(input('Please enter the years: '))
office = input('Please enter the office name: ')
# if-else condition
if (office.upper() == "IT"):
if (year >= 10):
print("10000")
else:
print("5000")
elif (office.upper() == "HR"):
if (year >= 10):
print("12000")
else:
print("6000")
elif (office.upper() == "ACCT"):
if (year >= 10):
print("15000")
else:
print("7500")
else:
print("The wrong office name entered")如果没有,您需要进一步澄清。
https://stackoverflow.com/questions/70497029
复制相似问题