Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

My code is not working, giving: AttributeError: 'int' object has no attribute 'upper'

I'm following this course on my laptop by typing exactly what Ken has on the workspace, but when I ran the code, I'm getting error, below is my code and the error:

CODE:

import datetime

answer_format = '%m/%d' link_format = '%b_%d' link = 'https://en.wikipedia.org/wiki/{}'

while True: answer = input("What date would you like? Please use the MM/DD format. Enter 'quit' to quit. ") if answer.upper() == 'QUIT': break

try:
    date = datetime.datetime.strptime(answer, answer_format)
    output = link.format(date.strftime(link_format))
    print(output)
except ValueError:
    print("That's not a valid date. Pleast try again")

ERROR:

{ Python } ยป python wiki.py /c/wamp64/www/Python 1 What date would you like? Please use the MM/DD format. Enter 'quit' to quit. 12/25 Traceback (most recent call last): File "wiki.py", line 9, in <module> if answer.upper() == 'QUIT': AttributeError: 'int' object has no attribute 'upper'

Steven Parker
Steven Parker
242,796 Points

When posting code, please use the instructions in the Markdown Cheatsheet pop-up link below the answer area. :arrow_heading_down:

2 Answers

This is because you are using Python 2. You might think you have the same version of Python that Kenneth has, but by default on a Mac or Linux (maybe Windows? I'm not sure) you have Python 2 while Kenneth is using Python 3. I recommend installing IDLE on the official Python website (if you still want to follow along on your machine), or following along on Workspaces.

Good luck! ~Alex

I'm actually using Python 3:

Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information.

If you run programs like this:

python hello.py

That will run "hello.py" with Python 2. To run programs with Python 3, run this command instead:

python3 hello.py

Good luck! ~Alex