-1
import datetime
today = datetime.datetime.now()
def bYear():
    age = input("How old are you: ")
    bYear = today.year - age
    print("Born in: " + bYear)
bYear()

I've started learning python a few days ago. Trying some 'stupid' stuff but I can't get this code working.

If i try to print the year with:

print(today.year)

the output is correct.

Error message:

<ipython-input-1-0eeb398a7422> in <module>
      5     bYear = today.year - age
      6     print("Born in: " + bYear)
----> 7 bYear()

<ipython-input-1-0eeb398a7422> in bYear()
      3 def bYear():
      4     age = input("How old are you: ")
----> 5     bYear = today.year - age
      6     print("Born in: " + bYear)
      7 bYear()

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

Im missing something here, any help?
bigbounty
  • 16,526
  • 5
  • 37
  • 65
rxdue
  • 181
  • 1
  • 11
  • Take a look at the other answers, some of them solve the problem. The one you gave the check to doesn't fix the second error. edit: make sure to select the one that works and explains both the problem and the solution. – Riley May 23 '20 at 22:56

3 Answers3

0

This might be because you are not changing your input type to int, and keeping it as a string, however, I do not know for sure. It would be nice if you provided your full stack trace. Try this:

import datetime
today = datetime.datetime.now()
def bYear():
    age = int(input("How old are you: "))
    bYear = today.year - age
    print("Born in: " + bYear)
bYear()

If you instead want your code to not crash (if someone inputs something other than an integer), try this:

import datetime
today = datetime.datetime.now()
def bYear():
    age = input("How old are you: ")
    if not age.isdigit():
        print("Invalid input!")
    else:
        age = int(age)
    bYear = today.year - age
    print("Born in: " + str(bYear))
bYear()
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Krishnan Shankar
  • 780
  • 9
  • 29
0

You need to convert your age as int as python takes input as str. And also change the bYear to str to print it using +.

import datetime
today = datetime.datetime.now()
def bYear():
    age = int(input("How old are you: "))
    bYear = today.year - age
    print("Born in: " + str(bYear))
bYear()
ksohan
  • 1,165
  • 2
  • 9
  • 23
0

When you use input it returns a string (letters). What you need is an integer. Using the int() function you can change strings into integers like below:

age = int(input("How old are you: "))

But wait, there's still an error! The issue is that you can only use + with things of the same type. You can't 'add' a string to an integer. To solve this use the str() function:

print("Born in: "+str(bYear))

So now it runs perfectly!

There is one more thing that may cause issues down the line, though. the variable today is not a 'global' variable, so if you make any changes to it inside of the bYear function, they wont carry over to the main program. To fix this put the line global today at the start of the bYear function.

Here's the full, working code:

import datetime
today = datetime.datetime.now()
def bYear():
    age = int(input("How old are you: "))
    bYear = today.year - age
    print("Born in: " + str(bYear))
bYear()
Riley
  • 179
  • 9