首页 文章

每个员工的python增量年份[关闭]

提问于
浏览
-4

我正在编写一个有6名员工的计划 . 我要问每个员工5年的年薪 . 我有代码,但我问的是如何输出:"Please enter employee 1 salary for year 1:"没有得到语法错误 . 我的代码是: salary = int(input("Please enter employee {} salary for year " .format(employee) +str(year) ': ')) 这是我的代码:

totalsalary = 0
salaryhigh = 0
salarylow = 10000000
employee = 0  
NumberOfEmployee = 7

for employee in range(1, NumberOfEmployee):
    for year in range(1,6):
        salary = int(input('Please enter employee {} salary for year' .format(employee) +str(year)))
        totalsalary = totalsalary + salary
        if(salary > salaryhigh):
            salaryhigh = salary
        if(salary < salarylow):
            salarylow = salary
    avesalary = float(totalsalary)/5
    print("Employee {} smallest salary is:" .format(employee) +str(salarylow))
    print("Employee {} highest salary is:" .format(employee) +str(salaryhigh))
    print("Employee {} average salary is:" .format(employee) +str(avesalary))
    print("**********")

3 回答

  • 1

    你没有告诉我们你如何尝试在字符串失败时添加冒号,但我猜你想要这样的东西:

    salary = int(input('Please enter employee {} salary for year' +str(year) +':' .format(employee)))
    

    那将显示如下所示的行:

    Please enter employee {} salary for year1:
    

    我猜你之前尝试的是在(年)之后添加冒号,但没有结肠周围的引号 . 有很多关于Python中格式化字符串的非常好的材料可以帮助你理解这一点 .

    以下是与您类似的一个示例:https://realpython.com/python-string-formatting/向下滚动到作者讨论 def greet 的位置 .

  • 0

    如果您正在使用字符串格式化,则无需连接,因此您只需在format方法中添加employee和year:

    'Please enter employee {} salary for year {}: '.format(employee, year)
    
  • 1

    似乎添加 : 不应该破坏它,但是如果您使用 f strings 似乎您的格式更清晰

    input(f'Please enter employee {employee} salary for year {year}: ')
    
     ...
    
     print(f"Employee {employee} smallest salary is: {salarylow}")
     print(f"Employee {employee} highest salary is: {salaryhigh}")
     print(f"Employee {employee} average salary is: {avesalary}")
    

相关问题