如何使用Python计算工资?

huangapple go评论63阅读模式
英文:

How to use python to calculate salaries?

问题

I have translated the code part for you:

work_hours = str(input('输入工作小时数'))
if (work_hours <= 40):
    salary = 20 * work_hours

if (work_hours > 40):
    over_salary = 30 * (work_hours - 40)
    reg_salary = 20 * 40 + over_salary

   print(reg_salary)

Please note that I've only translated the code portion as requested. If you have any specific questions about the code or need further assistance, feel free to ask.

英文:

I'm very, very new to code and my current class is sort of hitting us with all kinds of information at once. It's making a bit difficult to latch onto certain concepts. For this particular problem, our instructor wants us to write a code to calculate employee salaries based on hours worked. Here is the prompt: "A company wants a program that will calculate the weekly paycheck for an employee based on how many hours they worked. For this company, an employee earns $20 an hour for the first 40 hours that they work. The employee earns overtime, $30 an hour, for each hour they work above 40 hours."

He also gave us an example to work with: "If an employee works 60 hours in a week, they would earn $20/hr for the first 40 hours. Then they would earn $30/hr for the 20 hours they worked overtime. Therefore, they earned: ($20/hr * 40hrs) + ($30/hr * 20 hrs) = $800 + $600 = $1400 total."

Like I said, we were given a lot of information at once. I'm trying to use an if statement to make the code, but I know very well it's not written correctly. It's filled with all kinds of syntax errors. I'm just not sure what to do.

work_hours = str(input(&#39;Enter hours worked&#39; ))
if (work_hours &lt;= 40)
    salary = 20 * work_hours

if (work_hours &gt; 40)
    over_salary = 30 * (work_hours - 40)
    reg_salary = 20 * 40 + over_salary

   print(reg_salary)

答案1

得分: 0

str更改为int,并在if语句后面加上冒号:

work_hours = int(input('输入工作小时数: '))
if work_hours <= 40:
    salary = 20 * work_hours
elif work_hours > 40:
    over_salary = 30 * (work_hours - 40)
    salary = 20 * 40 + over_salary
print(salary)  # 如果输入60,得到1400

我已经将那一行work_hours = str(input('输入工作小时数: '))更改为work_hours = int(input('输入工作小时数: '))。通过更改这一行,我确保输入将被解释为数字。我还在您的if语句后面加上了冒号(:),这是我找到的唯一一个SyntaxError

英文:

Change that str to int and put colons after your if statements

work_hours = int(input(&#39;Enter hours worked: &#39; ))
if work_hours &lt;= 40:
    salary = 20 * work_hours

elif work_hours &gt; 40:
    over_salary = 30 * (work_hours - 40)
    salary = 20 * 40 + over_salary

print(salary) # If I input 60 I get 1400

You can see that I changed the line work_hours = str(input(&#39;Enter hours worked: &#39; )) to work_hours = int(input(&#39;Enter hours worked: &#39; )). By changing that line I made sure that the input would be interpreted as a number. I also put colons (:) after your if statements, which was the only SyntaxError I could find.

答案2

得分: 0

if`的两个分支必须生成相同的变量,以便始终有内容可打印。

工作小时数 = int(input('输入工作小时数: '))
if 工作小时数 <= 40:
工资 = 20 * 工作小时数
else:
超时工资 = 30 * (工作小时数 - 40)
工资 = 20 * 40 + 超时工资

print(工资)

或者,甚至更好的方式是将加班工资视为异常情况:

工作小时数 = int(input('输入工作小时数: '))
工资 = 20 * 工作小时数

if 工作小时数 > 40:
超时工资 = 10 * (工作小时数 - 40)
工资 = 工资 + 超时工资

print(工资)


<details>
<summary>英文:</summary>

Both branches of the `if` must produce the same variable, so you always have something to print.

work_hours = int(input('Enter hours worked: ' ))
if work_hours <= 40:
salary = 20 * work_hours

else:
over_salary = 30 * (work_hours - 40)
salary = 20 * 40 + over_salary

print(salary)

Or, even better, do the overtime as an exception:

work_hours = int(input('Enter hours worked: ' ))
salary = 20 * work_hours

if work_hours > 40:
over_salary = 10 * (work_hours - 40)
salary = salary + over_salary

print(salary)

答案3

得分: -1

work_hours = int(input('输入工作小时:')) # 这里输入应该是一个整数

if (work_hours <= 40): # 这里你漏掉了冒号标记

salary = 20 * work_hours

if (work_hours > 40): # 这里你漏掉了冒号标记

over_salary = 30 * (work_hours - 40)
reg_salary = 20 * 40 + over_salary

print("你的总工资是:", reg_salary, "$")
英文:

work_hours = int(input(&#39;Enter hours worked: &#39; )) # here the input should be an integer

if (work_hours &lt;= 40): #here you miss the ': mark

salary = 20 * work_hours

if (work_hours &gt; 40):#here you miss the ': mark

 over_salary = 30 * (work_hours - 40)
reg_salary = 20 * 40 + over_salary

print(&quot;your total salary is:&quot;,reg_salar,&quot;$&quot;)

I have found the some small errors in your code. You missed the ':' marks for if statement and the working hours should be insert as an integer. you entered as string. there are no arithmetic operations for strings.

huangapple
  • 本文由 发表于 2023年5月21日 02:05:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296687.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定