hi there am asking for help here:
the code below should inform me of how many items i have in my items list,
however it jams my code up: please assist me understand how to code it right:
i actually don't want to use the .format() and with len() method to achieve the results.
found in line 8: print(f"You now have {item}.len(shopping_list))"& " item(s) on you list!")
def add_to_list(item):
item = item.capitalize()
if item not in shopping_list:
shopping_list.append(item)
print(f"{item} was added to your shopping list!")
print(f"You now have {item}.len(shopping_list))"& " item(s) on you list!")
else:
print(f"You already have {item} in your list, can't add this again!")
add_to_list()
I have no idea what you think the & should do. It is for bitwise and between ints. In any case, you just need to correct the field expression and move the end-field marker } to enclose the entire field expression.
print(f"You now have {len(shopping_list)} item(s) on you list!")
Terry thanks a lot. it solve the issue.
Maybe you’ve figured this, maybe not…
You could add a little 'finesses" with:
...
if len(shopping_list) < 2:
print(f"You now have {len(shopping_list)} item on you list!")
else:
print(f"You now have {len(shopping_list)} items on you list!")
...
I would reject the duplication in a PR code review. ‘Finesse’ would be something like
num = len(shopping_list)
f"You now have {num} item{'s' if num != 1 else ''} on you list!"
Neat!
I was meaning a little ‘finesses’ with the displayed message, rather than with the coding of said.
Have a great day!
As long as we are nitpicking… surely the message should say your list
rather than you list
?
hi Terry and Rob, thank you guys, i have now had time to read through your help codes you gave.
in fact i have learned more again this morning.
it is great pleasure to have you guys dedicate to helping people like us here. i wont let you guys down i will extra hard to really get there.
i appreciated the codes a lot, they made sense
I have a hard time seeing large expressions in an f-string as finesse.
I’d rather:
num = len(shopping_list)
s = 's' if num != 1 else ''
f"You now have {num} item{s} on you list!"
It just feels tidier. f’a[i]’ doesn’t bother me, but I don’t like to take it much past that. This is partly because leaning on temporary variables is just easier to understand and debug, but not solely. There’s something about f-strings in particular.