2014-10-27 80 views
0
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 

「無法分配給運營商」的錯誤,告訴我,我會得到對已直接指定數值之間的變量,其餘相同的錯誤嵌套0​​。

我在做什麼錯了,在這裏?

+1

sepearate分成兩行drop&''或'或分號';' – nu11p01n73R 2014-10-27 04:22:42

+0

使用元組賦值'a,b = 1,2'導致'a == 1'和'b == 2' – IanAuld 2014-10-27 04:23:58

回答

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

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

謝謝非常適合及時回覆,下降並完美運作。我將不得不更多地瞭解運營商。 – user3517512 2014-10-27 04:30:50

+0

@ user3517512歡迎您:) – nu11p01n73R 2014-10-27 04:36:13

0

,而你是分配給變量你不需要使用and操作。但是你可以用它來檢查,如果條件(所有條件)已滿足elif statement.Try以下:

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 
0

我猜(:)希望這不是你的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 < 100000除非前面的測試失敗了,所以我們知道monthlySales>=10000是真實的,並再次測試它是多餘的。等