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
Michael Leece
2,622 PointsLooking for coding critic on first code practice please
Hey guys,
I've been that someone who sits back and reads other peoples questions and answers and never really put myself out there. For this Python shopping list Challenge I decided to write the code as a challenge without video guidance, then I also adapted what I had learnt and searched the internet on a couple syntax questions I had. (funny how everyone has at least asked the same question multiple times)
Anyways before getting side tracked - I want to see what I am doing well code wise, and where I can improve. I have been learning Python for a couple hours now so structure etc or any feedback at all would be appreciated!
#make a list to hold onto our items
#print out instructions on how to use the app
#ask for new items
#be able to quit the app
#print out the list
list = []
def instructions():
print("Please read and follow the instructions to add items to the shopping list")
instructions()
def quit_x():
try:
quit = input("To quit this application, please type \"quit\" or \"continue\" to add more items to the list: ")
except ValueError:
if quit != "quit":
print("Please enter using characters only")
else:
if quit == "quit":
quit = exit()
else:
if quit == "continue":
add_items()
def add_items():
more_items = True
while more_items == True:
try:
item = input("What item would you like to add to the shopping list?: ")
except ValueError:
print("Please enter using characters only")
else:
list.append(item)
more = input("Do you have more items? Y / N: ")
if more == "y" or more == "Y":
more_items = True
else:
more_items = False
print(list)
quit_x()
add_items()
Thank you for anyone who takes the time :)
P.S. I do know the instruction part of it could be better written - however that was not my keypoint I was focusing on haha
1 Answer
Andreas cormack
Python Web Development Techdegree Graduate 33,011 PointsHi Michael,
I like how your attempt to write the code without watching the video. I like your code, clear, nice separation of concerns. Few things, Your ValueError exceptions don't seems to be getting raised, also, i would convert the input to lowercase and then do your checks. I re wrote your code below with a few changes here and there.
#make a list to hold onto our items
#print out instructions on how to use the app
#ask for new items
#be able to quit the app
#print out the list
list = []
def instructions():
print("Please read and follow the instructions to add items to the shopping list")
def quit_x():
options = ["quit", "continue"]
# convert the input to lowercase
quit = input("To quit this application, please type \"quit\" or \"continue\" to add more items to the list: ").lower()
if quit not in options:
print("Please select a valid option")
quit_x()
else:
if quit != "quit":
add_items()
else:
exit()
def add_items():
#calling instructions here rather than above
instructions()
more_items = True
while more_items == True:
item = input("What item would you like to add to the shopping list?: ")
list.append(item)
# convert the input to lower case
more = input("Do you have more items? Y / N: ").lower()
if more.lower() == "y":
more_items = True
else:
more_items = False
print(list)
quit_x()
add_items()
Michael Leece
2,622 PointsMichael Leece
2,622 Pointsoh excellent, I appreciate the time you took for that. Taking a look at that, it allows it to be written more clearly without having to be convoluted with unnecessary code.
Thank you for letting me know about the ValueError, I'll do some more learning about that then, although in the rewrite it seems that I could write it without the ValueError lines which makes sense now reading through it.
Thank you again for the time you took, I shall take this knowledge into the next challenges :)
-Michael