Problems with input results after using an "int()"

Asked

Viewed 3,749 times

2

Code example:

idade = input("Qual a sua idade? ")    
int(idade)

if idade >= 45:    
    print("Tá velho cara, já era!")    
else:    
    print("A vida está só começando rapaz!")

Error found:

Qual a sua idade? 45
Traceback (most recent call last):
  File "C:/Users/Lucio/Desktop/ProgramasPython.py", line 6, in <module>
    if idade >= 45:
TypeError: '>=' not supported between instances of 'str' and 'int'
  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

3 answers

3

In the second line you are converting the contents of the variable idade for whole and discarding the result, then when you check you think it is already a whole, but it is still a string. I imagine you wanted to store this somewhere. And I imagine you think just doing the operation on the variable changes her value, but it doesn’t, you just manipulate her value.

But the correct thing would be to create another variable since they are different types. And the error caused shows why it is better not reuse variable with different types. In Python it would work to play in the same variable, but it’s not nice to do this. The good part is that you don’t even have to do this, you can read the data and convert at once and only save the converted value, like this:

idade = int(input("Qual a sua idade? "))
if idade >= 45:
    print("Tá velho cara, já era!")
else:
    print("A vida está só começando rapaz!")

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

This code can still fail, it can be written differently, but basically, to start learning, this is it.

2

You thought that int(idade) would transform the variable idade in an integer, gives?

Well, you just saw that that’s not what happened. The builder int(str) when receiving a string creates an object of the type int whose decimal value is the one that would be shown in the string. This was actually done in your code. However, without pointing it at any variable, this object will be created and lost. To not lose, you need to assign this value to a variable. So you could even do:

idade = input("Qual a sua idade? ")    
idade = int(idade)

But, really? You want to read an entire number, so why not read an entire one already? It looks like this:

idade = int(input("Qual a sua idade? "))

0

Typeerror: '>=' not supported between instances of 'str' and 'int'

This error happens because the relational operator (>=) is being used between the string and the integer.

To correct just add double quotes on the whole ("45") as example below:

age = input("What is your age? ")
int(age)

if age >= "45":
print("Old man, it’s over!")
Else:
print("Life is just beginning boy!")

Browser other questions tagged

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