在Python中将字符串转换为Int

时间:2017-03-07 02:21:33

标签: python function class

下面的代码,我试图在我的give_raise方法中将字符串转换为int。我知道这可能是我想念的简单事情,但我很难过。将字符串转换为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 个答案:

答案 0 :(得分:-1)

代码行int(bonus)不会将整数分配给变量。您需要将其分配给变量并在计算中使用它。

IE

integer_bonus = int(bonus)
annual_salary = integer_bonus + annual_salary

注意:我换了你的annual_salary作业。你的方式很尴尬。