【问题标题】:What is the mistake in the given code?给定代码中的错误是什么?
【发布时间】:2018-08-02 13:20:26
【问题描述】:

一家公司决定给员工发奖金。男性员工可获得 5% 的奖金,女性员工可获得 10% 的奖金。如果员工的工资低于 10000 则员工获得 2% 的额外奖金。

计算工资和奖金。

我的代码如下所示:

a = input('Enter your name here: ')
b = int(input('your salary here: '))
c = input('your gender here (M/F) : ')
if b < 10000 and c == 'M':
    print(str(a) + ' your salary with bonus is ' + str(b * 1.07))
elif b < 10000 and c == 'F':
    print(str(a) + ' your salary with bonus is ' + str(b * 1.12))

if b >= 10000 and c == 'M':
    print(str(a) + ' your salary with  bonus is '+ str(b * 1.05))

elif b >= 10000 and c=='F':
        print(str(a) + ' your salary with  bonus is '+ str(b * 1.1))

【问题讨论】:

  • 什么错误?请提供一些示例输入,然后告诉我们您得到什么输出以及您期望什么输出。 This page 也可能有帮助。

标签: python python-3.x


【解决方案1】:

好的,你不能只输入:

if b<10000 and c=='M'  :
    # code
else:
b<10000 and c=='F'

如果要检查两个条件,则需要两个 if,如下所示:

if b<10000 and c=='M'  :
    # code
else:
  if b<10000 and c=='F' :
    # code

【讨论】:

  • 该百分比适用于基本工资。所以百分比的加法部分是可以的。我犯了一些语法错误,请告诉我!
  • 谢谢。所以我需要直接使用elif。对吗?
  • @AXELROD 是的,elif 也可以。如果您觉得我的回答有用,请考虑接受。
猜你喜欢
  • 2019-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-03
  • 2011-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多