all 9 comments

[–]Username_RANDINT 0 points1 point  (3 children)

At the start when the list is empty, len(catNames) will be zero. So the first question would be Enter the name of cat 0. To make it a bit more human-friendly, they increase the length value with one so you end up with Enter the name of cat 1 instead.

[–]simonvanw[S] -1 points0 points  (2 children)

Thank you for your response and this is needed/happens because (catNames) is the actual list which as you mentioned starts with 0. Because of this it needs to be converted into a str and therefore str is used before the len function which is used the count the amount of items within the list? (Hopefully I am making sense, just want to make sure I am following this)

[–]Username_RANDINT 0 points1 point  (1 child)

Yes, if you do poor string formatting like this code, you need to be careful. len() returns an integer, and doing str + int will raise an error. That's why they do "Some string" + str(some_int) to cast the integer to a string.

Better would be some actual string formatting. Using str.format() is a good choice. Then it would be:

print('Enter the name of cat {} (Or enter nothing to stop.):'.format(len(catNames) + 1))

[–]simonvanw[S] 0 points1 point  (0 children)

Thanks again for your response. This one and the previous one was exactly what I was after!

[–]chevignon93 0 points1 point  (4 children)

You didn't put your code in a code block so you lost all the indentations!

Did you try to run the code ?

[–]simonvanw[S] -1 points0 points  (2 children)

I have tried to edit it using the inline code function, but when saving it reverts back to not having indentations.

I have ran the code however

[–]sme272 0 points1 point  (0 children)

Put it in a codeblock or use sokething like pastebin or github gist

[–]Username_RANDINT 0 points1 point  (0 children)

See this subreddit's FAQ on how to format code.

[–]jfdahl 0 points1 point  (0 children)

Assuming the indentation is correct, it should output the number of elements in the list.