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
boi
14,242 PointsPython Basics ------ Create an app "Shopping List" challenge. Need a strong analysis on my code PLEASE HALP Thank you.
Hello sir/Madam, I am having a few questions on my mind that need consultation. I truly appreciate the time you take to guide me.
This is my code, and the questions are below my pasted code.
def s():
print()
def show_cart():
item = 1
if not cart:
print("No Items in your cart")
return
print(" These are the item(s) in your cart:")
for x in cart:
print("\n Item {} = {}".format(item,x))
item += 1
cart = []
name = input("hello, can I have your name: ")
s()
message = print("""Hello {},\nInput "Help" for instructions""".format(name))
s()
while True:
grocery = input("Add items to cart >")
s()
if grocery.upper() == "HELP":
print("""Hello {},\nTo end the shopping input "Done"\nTo check your shopping cart input
"Show cart"\nTo remove an item from you cart input "Remove" """.format(name))
continue
elif grocery.upper() == "DONE":
show_cart()
break
elif grocery.upper() == "SHOW CART":
show_cart()
s()
continue
try:
if grocery.upper() == "REMOVE":
remove = input("what item you want removed? :")
s()
cart.remove(remove)
print("Total number of items in cart = {}".format(len(cart)))
continue
except ValueError:
print("The item you named is not on the list BOII")
continue
cart.append(grocery)
s()
print("Total number of items in cart = {}".format(len(cart)))
Question 1 :
Why did Mr. Craig make a function "Show_Help" instead of just doing what I did? I don't understand the use of that function on this matter.
Question 2 :
In my def function "show_cart" I used the "return" statement and "if not" statement without totally understanding their way of use. I can't totally understand how to read the "if not" statement and the way it is being used here, need clarification on these statements.
Question 3 :
I read about "break" and "continue" statements online, the sources tell me that these statements are a "bad programmers" method of coding, I can't understand what that means.
Question 4 :
How can I make this code better than it is? Can it be smaller or more user-friendly? And finally, what would you rate my code out of 10?
Note-- I hope this is not an annoying or improper way of asking for help but I just can't understand some things. ( Am not very bright). I appreciate any feedback on my questions and Thank you.
1 Answer
YZ L
6,184 Points1: thats just for practice :)
2: the arguments after 'íf' are return a boolean value (True or False) if True it runs the indented code and if False it skips it
now, empty values are known as 'falsey' so. 'if not c' means if c is not true (and it is't) then return true and run
return is what should result when the function is called
3: niether can I
4: return should be the last line of the block and have an argument (your questions are fine, getting used to python lingo takes time) good luck!