Click here to Skip to main content
15,912,665 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have this code in python that i want to counter your age

x=input("Give me your birthdate")
y=(2018)- (x)
if y>=18:
print("You are adult")


else:
z=18-y
print("You will be adult in", z, years)

and it appear me this error

TypeError: unsupported operand type(s) for -: 'int' and 'str'

What I have tried:

i tried to see in the internet but i didnt found anything
Posted
Updated 15-Nov-18 6:10am

That's not good - "18 years old" as an adult is not just a case of "year - x".
See here: Working with Age: it's not the same as a TimeSpan![^] - it's C# based, so you can;t use it directly, but it's pretty simple stuff so you should be able to work out what to do in Python.
 
Share this answer
 
You need to convert the input into a date type and get the year from that. See datetime — Basic date and time types — Python 3.7.1 documentation[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900