Note: I am fairly new to this but can't figure this problem out. I want to find the age of people as a question and for them to enter it themselves. The problem is, is when I want to subtract c - b it says error, because both of them are strings and you cannot subtract strings. How can I convert these strings into numbers that let me add and subtract them. As if I said it was year 2000 and I was born 1999, it would say you are 20001999 instead of 1 years old. Is there another way to right this or a way to convert these strings into numbers? Much would be apreciated.

a = raw_input("Enter name: ")
     
b = raw_input("Enter the year you were born: ")

c = raw_input("Enter the year it is now: ")

print "You,",a,",are ",c - b,"years old."

You cannot subtract strings, but you can cast them to integers and then it will work.

a = raw_input("Enter name: ")
     
b = int(raw_input("Enter the year you were born: "))

c = int(raw_input("Enter the year it is now: "))

print "You,",a,",are ",c - b,"years old."

Use the built-in function int() :

lhs = int(raw_input("Number: "))
rhs = int(raw_input("Another number "))
print "%d plus %d is %d"%(lhs, rhs, lhs+rhs)

If you wish to have people enter a date, then read about datetime module, paying particular attention to the strftime() and strptime() methods.

Thanks! Just what I wanted to see.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.