Home > Enterprise >  Creating a function to add 2 string numbers and then return as a integer
Creating a function to add 2 string numbers and then return as a integer

Time:12-09

I have the below code, what i have to do is add the 2 strings together to make 150 and then convert that string into a integer i have used the code below but not sure how ti add 2 strings together and then return it, help thanks

def sum_to_int("50", "100"):
    numbers = "50"
    numbers1 = "100"

    myarr = numbers.split(" ")
    myarr2 = numbers1.split(" ")

    print(myarr)
    print(myarr2)

    myarr = [int(i) for i in myarr]
    myarr2 = [int(i) for i in myarr2]

    print(myarr, myarr2)
    

result = sum_to_int
print(sum_to_int(numbers, numbers1))

CodePudding user response:

def sum_to_int(str_1 ,str_2):
    #first convert string values to int
    int_1 = int(str_1)
    int_2 = int(str_2) 

    #just plus integers
    total = int_1   int_2 

    return total
    #use -> return int(str_1) int(str_2)

print(sum_to_int("100","50"))

CodePudding user response:

Be simple :

def sum_to_int(numbers1, numbers2):
    return int(numbers1)   int(numbers2)

print(sum_to_int("50","100"))
# 150
  • Related