每位员工的python增量年

时间:2018-10-25 17:43:34

标签: python python-3.x

我正在编写一个有6名员工的程序。我必须问每位雇员5年的年薪。我没有代码,但是我想问的是如何输出:“请输入员工1年的薪水: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 个答案:

答案 0 :(得分: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的地方。

答案 1 :(得分:1)

如果您使用的是字符串格式,则无需同时进行连接,因此您只需在格式方法中添加员工和年份:

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

答案 2 :(得分:0)

看起来像添加sed -i '' -E 's/src=\"*.+http/src=\"http/g' myfile.html 应该不会破坏它,但是如果您使用:

,看起来格式会更清晰
f strings