【问题标题】:Attempting to assign a variable to an value causes "Cannot assign to operator" error尝试将变量分配给值会导致“无法分配给操作员”错误
【发布时间】:2014-10-27 04:19:19
【问题描述】:
    def main():
    bonus()
def bonus():
    #Dollars from sales are input, then time worked,
    #then the salary and possible bonus is added
    #to the calculated commission based on the earned commission rate
    monthlySales=int(input('How much money did your employee make in sales?',))
    if monthlySales<10000:
        commRate=0
    elif monthlySales>=10000 and monthlySales<100000:
        commRate=0.02
    elif monthlySales>=100001 and monthlySales<500000:
        commRate=0.15 and monthlyBonus=1000
    elif monthlySales>=500001 and monthlySales<1000000:
        commRate=0.28 and monthlyBonus=5000
    elif monthlySales>1000000:
        commRate=0.35 and monthlyBonus=100000
    yearsWorked=int(input('How many years has your employee worked here? Round down to the nearest year.',))
    if yearsWorked>=5 and monthlySales>=100000:
        extraBonus+1000
    elif yearsWorked<1:
        monthsWorked=int(input('How many full months has your employee worked here?',))
        if monthsWorked<3:
            print('Your employee has not worked here long enough to qualify for a bonus.')            
main()

我想做的是制定一个计划,在该计划中,预先确定的佣金率取决于员工在该计划中的销售额。

我收到“无法分配给操作员”错误

commRate=0.35 and monthlyBonus=100000

,这告诉我在 if 嵌套中直接分配数值的其余变量将得到相同的错误。

我做错了什么,在这里?

【问题讨论】:

  • 分成两行删除and或使用分号;
  • 使用元组赋值 a, b = 1, 2 导致 a == 1b == 2

标签: python operator-keyword


【解决方案1】:
elif monthlySales>=100001 and monthlySales<500000:
    commRate=0.35 ; monthlyBonus=100000

elif monthlySales>=100001 and monthlySales<500000:
    commRate=0.35
    monthlyBonus=100000

【讨论】:

  • 非常感谢您的及时回复,放下并完美运行。我必须了解更多关于运营商的信息。
【解决方案2】:

在将值分配给变量时,您不需要使用and 运算符。但是可以用elif语句来检查条件(所有条件)是否满足。试试下面的方法:

elif monthlySales>=100001 and monthlySales<500000:
    commRate=0.15
    monthlyBonus=1000
elif monthlySales>=500001 and monthlySales<1000000:
    commRate=0.28
    monthlyBonus=5000
elif monthlySales>1000000:
    commRate=0.35
    monthlyBonus=100000 

【讨论】:

    【解决方案3】:

    在 Python 中,赋值不能发生在表达式内部,这与 C 不同,这就是错误的原因。这是为了防止在打算进行比较时意外分配。请在第 5.7 节的最后一段中阅读它:

    https://docs.python.org/2/tutorial/datastructures.html

    【讨论】:

      【解决方案4】:

      我猜(希望 :) 这不是您的 bonus() 函数的完整列表,因为它实际上并没有返回或打印它计算的任何数据。但我注意到您需要处理该函数中的一些事情。

      extraBonus+1000 行对未定义的变量 (extraBonus) 执行计算,然后它不会将结果存储在任何地方。

      if...elif 部分中的前两个条件没有为monthlyBonus 设置值;您需要在稍后在函数中使用 monthlyBonus 之前修复它。

      另外,if...elif 部分执行冗余测试,因此可以简化:

      monthlyBonus = 0
      if monthlySales < 10000:
          commRate = 0
      elif monthlySales < 100000:
          commRate = 0.02
      elif monthlySales < 500000:
          commRate = 0.15; monthlyBonus = 1000
      elif monthlySales < 1000000:
          commRate = 0.28; monthlyBonus = 5000
      else:
          commRate = 0.35; monthlyBonus = 100000
      

      除非之前的测试失败,否则我们不会到达elif monthlySales &lt; 100000,因此我们知道monthlySales&gt;=10000 是正确的,再次测试它是多余的。等等。

      【讨论】:

        猜你喜欢
        • 2017-02-13
        • 1970-01-01
        • 2012-07-07
        • 1970-01-01
        • 2016-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多