Python if elif 代码创建薪水

标签 python python-2.7 python-3.x

我正在尝试使用 Python 编码在用户输入上述小时数时创建薪水。每周工作时间低于 40 小时的标准工资为每小时 9.25 美元。任何超过 40 小时的事件都会奖励 150% 的奖金(即 9.25 的 150%)。我还应该能够以分数形式输入小时数,并能够打印工资支票作为输出(其中说明工作小时数、每小时工资、收到的奖金(如果有)以及总工资)

到目前为止,我已经能够成功使用 if else 语句来获得最终结果,但我不知道如何接受分数并在输出中打印整个薪水。我对 Python 还很陌生,而且非常喜欢它。有人可以帮我即兴编写代码并帮助我处理分数并打印整个薪水吗?

这是到目前为止我的代码。

hours = int(input('Please enter the number of hours...'))

if hours <= 40:
    hourlyWage = hours*(9.25)

elif hours > 40:
    hourlyWage = hours*(9.25*1.5)

print('Your salary is ${0}'.format(hourlyWage))

非常感谢您的帮助!

最佳答案

hours = int(input('Please enter the number of hours...'))

hourlyWage = 9.25
bonus = 0.5
bonusThreshold = 40
bonusHours = max(0, hours - bonusThreshold)
regularSalary = hourlyWage * hours
bonusSalary = bonusHours * hourlyWage * bonus
totalSalary = regularSalary + bonusSalary

print('Worked: {0} hours.'.format(hours))
print('Pay per hour: ${0}.'.format(hourlyWage))
print('Bonus received: ${0}'.format(bonusSalary))
print('Total salary: ${0}'.format(totalSalary))

关于Python if elif 代码创建薪水,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32573926/

相关文章:

python - 如何在终止子进程后自动从 Popen 获取返回码?

python - 在数据类中创建类变量的正确方法

python - 使用 seaborn 为绘图添加标签

python - argparse:store_true 和 int 同时存在

python - 在 Marshmallow Schema 中以编程方式定义字段

javascript - django无法在 View 中创建常规对象,返回500错误

python-2.7 - 属性错误 : 'module' object has no attribute 'merge_all_summaries'

python - 使utf8在文件中可读

python - 在 python 中使用 re.sub 捕获替换的文本

python - ModuleNotFoundError : No module named 'pandas.io.formats.csvs'