JavaEar 专注于收集分享传播有价值的技术资料

在Python中将字符串转换为Int (Converting a String to an Int in Python)

The code below, I am trying to convert a string to int in my give_raise method. I know it's probably something simple I am missing, but I am stumped. What would be the proper syntax to convert the string to int and add that bonus to my annual salary total?

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个回答

    最佳答案

  1. 英文原文

    The line of code int(bonus) does not assign the integer to a variable. You need to assign it to a variable and use it in your calculation.

    IE

    integer_bonus = int(bonus)
    annual_salary = integer_bonus + annual_salary
    

    Note: I swapped your annual_salary assignment. The way you had it was awkward.


    中文翻译

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

    IE

      integer_bonus = int(bonus)
    annual_salary = integer_bonus + annual_salary
     

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

    The line of code int(bonus) does not assign the integer to a variable. You need to assign it to a variable and use it in your calculation.

    IE

    integer_bonus = int(bonus)
    annual_salary = integer_bonus + annual_salary
    

    Note: I swapped your annual_salary assignment. The way you had it was awkward.

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

    IE

      integer_bonus = int(bonus)
    annual_salary = integer_bonus + annual_salary
     

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