0

This is my code :

name = input("What's your name? ")
print("Nice to meet you " + name + "!")
age = input("Your age? ")
print("So, you are already " + age + " years old, " + name + "!")
if age > 50:
    print ("you are to old thx ")
else:
    print ("nice age using your apportunity ")

This is my result with error :

What's your name? daffa
Nice to meet you daffa!
Your age? 12
So, you are already 12 years old, daffa!
Traceback (most recent call last):
   File "C:\Users\murtadho\AppData\Local\Programs\Python\Python37\x.py", line
6, in <module>
     if age > 50:
TypeError: '>' not supported between instances of 'str' and 'int'

what happened in here?

Arkistarvh Kltzuonstev
  • 6,572
  • 7
  • 24
  • 47
daffa faiz
  • 29
  • 3
  • 1
    Hello, welcome to StackOverflow! If you already haven't, please read [how to ask](https://stackoverflow.com/help/how-to-ask). What have you tried until this point ? –  Jun 20 '19 at 08:51
  • Error is clear, they are different types you need to cast your age to an `int`, `input` returns a `str` – EdChum Jun 20 '19 at 08:52
  • 2
    convert the input age to int using `int(age)` , it should work – arajshree Jun 20 '19 at 08:52
  • Possible dupplicate of [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) and [How to convert strings into integers in python](https://stackoverflow.com/questions/642154/how-to-convert-strings-into-integers-in-python) – Maxim Irrigad Jun 20 '19 at 09:16

1 Answers1

1

try this : as a solution ; i already update your code to take in considerations the type of the age only an integer

name = input("What's your name? ")
print("Nice to meet you " + name + "!")
while True:
    try:
        age = int(input("Your age? "))
        break
    except Exception as e:
        print("please enter a valid age")
print("So, you are already " + str(age) + " years old, " + name + "!")

if age > 50:
   print ("you are to old thx ")

else:
   print ("nice age using your apportunity ")
Ghassen
  • 718
  • 6
  • 14
  • Try to explain the solution, the OP did not learn anything from this answer probably. – Maxim Irrigad Jun 20 '19 at 08:56
  • why i must get " age" in interger ?, what if im using string to compare – daffa faiz Jun 20 '19 at 08:57
  • You can compare as strings but it probably does not work as you expect, the problem in your code is that you can not compare integrers with strings, it is like comparing apples with pears. The python interpreter just does not know what do you expect from that statement. String comparison is alphabetical – Maxim Irrigad Jun 20 '19 at 08:59
  • i already update this code to take in considerations the type of the age only an integer – Ghassen Jun 20 '19 at 09:05
  • if im comparing string in my code, i must include str(var) like this ? – daffa faiz Jun 20 '19 at 09:06
  • you can only get an int integer so if you make invalid type an error message occur and the prompt re ask you to give a valid age – Ghassen Jun 20 '19 at 09:06
  • thx for help my quest eheheheh – daffa faiz Jun 20 '19 at 09:07
  • no, you are comparing `age`, that is a string, and `50`, that is an integer, `"50"` is a string. The problem is that you have to make your `input()`, that is a string, a number, to use the number comparison. You do it with `int(...)` – Maxim Irrigad Jun 20 '19 at 09:08
  • the best why is to compare int age with int not an str – Ghassen Jun 20 '19 at 09:08
  • @Ghassen The way that gives him the expected result is integer comparison, that is true, but your answer does not explain why his snipped does not work, it "fixes it" with a "try this, I also included something else..." , please update it – Maxim Irrigad Jun 20 '19 at 09:14
  • i read some post this input can use on str an int isn't it ? – daffa faiz Jun 20 '19 at 09:15