How to convert a string variable to int?

Asked

Viewed 65,891 times

10

As I correctly declare a value whole, for he is returning that my variables are strings.

n1 = input("informe um número ")
n2 = input("informe um número ")

soma = n1 + n2
print ("O resultado da soma de ", soma)

inform a number 25
inform a number 25
2525

>>>

6 answers

16


As already mentioned in the other answers, the return of function input() is the type string, the same applies to the function raw_input() in Python 2.x.

numero1 = int(input("Informe um numero: "))
numero2 = int(input("Informe um numero: "))

Also consider treating possible exceptions which may occur, for example exception ValueError which is thrown when a function receives an argument that has the right type, but an invalid value.

Behold:

try:
    numero1 = int(input("Informe um numero: "))
    numero2 = int(input("Informe um numero: "))

    soma = numero1 + numero2
    print ("{0} + {1} = {2}".format(numero1, numero2, soma))

except ValueError:
    print("Somente numeros sao aceitos. Tente novamente.")

See demonstração

  • ut84, treating Value Error, example when setting int value for variable x only that a real value is inserted in this variable ? And thank you so much for your explanation.

  • @leogif In this case, instead of int, should be used float. Behold that example.

7

  • bigown, vlw Thank you very much. How the def function works ?

  • bigown, can you refer me a book on python 3 for beginners ? And thank you so much for the help

  • 1

    I don’t like to indicate something unless I trust a lot and in Python I don’t know anything that trusts 100% (it doesn’t mean I don’t have it, I just don’t know it well). The right place to find this information is ours tag on the topic. I don’t know if the directions there are good but have a chance: http://answall.com/tags/python/info

  • n1 = input("enter your 1st Quarter note ") N2 = input("enter your 2nd Quarter note") N3 = input("enter your 3rd Quarter note") N4 = input("enter your 4th Quarter note ") media = float((n1 + N2 + N3 + N4)) / int(4) print("The average is",media) enter your 1st Quarter note 2 enter your 2nd Quarter note 2 enter your 3rd Quarter note 2 enter your 4th Quarter note 2 The average is 555.5

6

input recognizes string values. so you should do the following:

soma = int(n1) + int(n2)

int(), makes the values numbers

  • Arnaldo Badin, vlw Thank you very much. How the def function works ?

  • Arnaldo Badin, can you point me to a book on python 3 for beginners ? And thank you so much for your help

  • the function is used to perform something just calling her, example: def printX(): x = 10 print(x)............... I recommend you see this guy here: https://www.youtube.com/watch?v=oVp1vrfL_w4&list=PLQVvvaa0QuDe8XSftW-RAxdo6OmaeL85M this guy is sensational, watching this you get pro in python ;)

0

You must define which type of your variable, in Python is no different and the types are:

int = número inteiro
float = número decimal
str = texto

para o seu problema devemos colocar


n1 = int(input("informe um número "))
n2 = int(input("informe um número "))

soma = n1 + n2
print ("O resultado da soma de ", soma)

0

You could set the int value for the input from the beginning:

n1 = int(input("informe um número "))
n2 = int(input("informe um número "))

soma = n1 + n2
print ("O resultado da soma de ", soma)

or could set the int value later on the variable:

n1 = input("informe um número ")
n2 = input("informe um número ")

soma = int(n1) + int(n2)
print ("O resultado da soma de ", soma)

-1

a simple way for this below.

while True:
    try:
        variavel = int(input('Digite um número: '))
        break

    except ValueError:
        False
  • 2

    Gedson, why is there a False in the except? Wouldn’t it be better to use the pass as you wish to ignore the exception?

  • Hi Anderson! So I’m learning Python now. I made this example so that the user doesn’t leave WHILE until they enter a correct number. With PASS WHILE is stopped.

  • No, pass does not while being interrupted, the break that would do it.

  • the code is as follows. the person type anything and the BRAKE only occurs if what was typed is an INTEGER, if there is an error here comes FALSE and the WHILE continues.

  • 1

    That’s exactly what I’m saying. The False inside except has no function in the code. If it has no function, it should be the pass. The pass does not serve to close the loop, but rather to inform that nothing will be done in that code block. I gave more details about this in What is the real usefulness of the pass in this case?

  • Now yes! Got it I did the test here with PASS instead of FALSE and it works too! hehehehe I was very confused. Because I would change the BREAK to PASS and one worked

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.