2017-03-07 17 views
0

下面的代碼中,我試圖在我的give_raise方法中將字符串轉換爲int。我知道這可能是我錯過的簡單東西,但我很難過。將字符串轉換爲int並將該獎金添加到我的年薪總額中的正確語法是什麼?將字符串轉換爲Python中的Int

class Employee(): 
    """Stores an employee's data""" 

    def __init__(self, first_name, last_name, annual_salary): 
     """Employee values""" 
     self.first_name = first_name 
     self.last_name = last_name 
     self.annual_salary = annual_salary 

    def give_raise(self, annual_salary = 40000): 
     """Sets a default salary with the option to add more""" 
     choice = input("Do you want to add a bonus to the annual salary of more than 5000 dollars? y\n") 
     if choice == 'n': 
      annual_salary += 5000 
     else: 
      bonus = input("Please enter your bonus amount: ") 
      int(bonus) 
      annual_salary + bonus = annual_salary 

     print(annual_salary) 

my_Employee = Employee('Harry', 'Scrotum', 40000) 
my_Employee.give_raise() 

回答

-1

代碼行int(bonus)未將整數賦值給變量。您需要將其分配給一個變量並在計算中使用它。

IE

integer_bonus = int(bonus) 
annual_salary = integer_bonus + annual_salary 

注:我換你的annual_salary分配。你的方式很尷尬。

+0

謝謝Doug!工作完美。現在到練習的第2部分,爲它寫一個測試用例 –