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 trialAnthony Dills
Python Development Techdegree Student 5,336 PointsI have an issue in my code when trying to input a published date for a new book entry
In my terminal testing the code, adding a new book to the list, when asked for the published date I get the error message for correct date format. I have tried all types of ways to enter the date but nothing clears it to move on to the rest of the code. I must be over looking something in my code can anyone point it out?
def clean_date(date_str):
months = ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December']
split_date = date_str.split(' ')
try:
month = int(months.index(split_date[0]) + 1)
day = int(split_date[1].split(',')[0])
year = int(split_date[2])
return_date = datetime.date(year, month, day)
except ValueError:
input('''
\n****** DATE ERROR ******
\rThe date format should include a valid Month day, year from the past.
\rEx: January 13, 2003
\rPress enter to try again.
\r************************''')
return
else:
return return_date
def app():
app_running = True
while app_running:
choice = menu()
if choice == '1':
title = input('Title: ')
author = input('Author: ')
date_error = True
while date_error:
date = input('Published Date (Ex: October 25, 2017): ')
date = clean_date(date)
if type(date) == datetime.date:
date_error = False
price_error = True
while price_error:
price = input('Price (Ex: 25.64): ')
price = clean_price(price)
if type(price) == int:
price_error = False
new_book = Book(title = title, author = author,
published_date = date, price = price)
session.add(new_book)
session.commit()
print('Book Added!')
time.sleep(1.5)
elif choice == '2':
for book in session.query(Book):
print(f'{book.id} | {book.title} | {book.author}')
input('\nPress enter to return to the main menu.')
elif choice == '3':
id_options = []
for book in session.query(Book):
id_options.append(book.id)
id_error = True
while id_error:
id_choice = input(f'''
\nId options: {id_options}
\rBook ID: ''')
id_choice = clean_id(id_choice, id_options)
if type(id_choice) == int:
id_error = False
the_book = session.query(Book).filter(Book.id == id_choice).first()
print(f'''
\n{the_book.title} by {the_book.author}
\rPublished: {the_book.published_date}
\rPrice: ${the_book.price / 100}
''')
sub_choice = submenu()
if sub_choice == '1':
pass
elif sub_choice == '2':
pass
elif choice == '4':
pass
else:
print('GOODBYE')
app_running = False
1 Answer
jb30
44,807 PointsIn the line split_date = date_str.split(' ')
, you have two spaces in the quotes. Try removing one of the spaces, or formatting your input date with two spaces between month and day and two spaces between the comma after the day and year.
Anthony Dills
Python Development Techdegree Student 5,336 PointsAnthony Dills
Python Development Techdegree Student 5,336 PointsThankyou ! I knew I was looking over something, much appreciated