Sonofmetal
Sonofmetal

Reputation: 5

How do I change a string to an integer?

Right now I have a bit of a kink with my code. CODE:

def main(a,b):
    c=a+b
    print("Your answer was: %s"% c)
    input()

def launch():
    print("Please set integer a and integer b!")
    intA=input("Integer A: ")
    intB=input("Integer B: ")
    input("Press return to continue!")
    main(intA, intB)

What I need help with is converting intA&intB in to ACTUAL integers. Because when I run this piece of code I get: 3030... If anyone could help that would be really appreciated! Thanks! :)

Upvotes: 0

Views: 105

Answers (2)

Sheng
Sheng

Reputation: 3555

change

intA=input("Integer A: ")
intB=input("Integer B: ")

to

intA=int(input("Integer A: "))
intB=int(input("Integer B: "))

Upvotes: 1

pcalcao
pcalcao

Reputation: 15988

You can use int:

c = int(a)+int(b)

Of course, you can do this conversion even before calling your main method.

The point is: int tries to convert the parameter to a integer, conversely, you could use str to convert a number to a string.

Upvotes: 0

Related Questions