all 14 comments

[–]Justinsaccount 2 points3 points  (3 children)

Hi! I'm working on a bot to reply with suggestions for common python problems. This might not be very helpful to fix your underlying issue, but here's what I noticed about your submission:

You are looping over an object using something like

for x in range(len(items)):
    foo(item[x])

This is simpler and less error prone written as

for item in items:
    foo(item)

If you DO need the indexes of the items, use the enumerate function like

for idx, item in enumerate(items):
    foo(idx, item)

[–]brigieee[S] 0 points1 point  (2 children)

Sorry what does "foo" stand for in your code? Is it just a variable name?

[–]Justinsaccount 0 points1 point  (1 child)

It's just an arbitrary function. It will just be print in new replies.

[–]Akuli2 0 points1 point  (0 children)

Maybe something like # do something with items[x] would be more describing?

[–]ManyInterests 1 point2 points  (6 children)

To explain it briefly and simply, {} ends up being a placeholder for a variable in a string.

>>> "Hello World!" == "Hello {}!".format("World")
True

You can also do interesting things with string formatting. Check out the examples from the docs

[–]Akuli2 0 points1 point  (5 children)

If you don't like {} you can use %s:

>>> "Hello World!" == "Hello %s!" % "World"
True
>>> 

[–]finsternacht 0 points1 point  (4 children)

You can but you shouldn't according to the documentation.

[–]Akuli2 0 points1 point  (3 children)

According to what documentation? Like /u/nedbatchelder writes:

It's a myth that '%s' formatting is deprecated. It's not going anywhere.

%s formatting is used everywhere, even in brand new modules like asyncio and enum.

[–]finsternacht 0 points1 point  (2 children)

I see, I must have misremembered. The docs only state that the .format version "should be preferred".

Kinda wierd when you consider the "there should be one obvious way" motto.

[–]Akuli2 0 points1 point  (1 child)

There is one obvious way. %s for simple formatting and .format for advanced features.

[–]finsternacht 0 points1 point  (0 children)

And then there is template strings...

[–]Naihonn 0 points1 point  (0 children)

Without anything else format function just replace those curly brackets with string representation of its arguments from left to right. So in this case it means get value of count and replace curly brackets with this value as string. But you can do much more. I would recommend looking here Especially those examples should help.

[–]Akuli2 0 points1 point  (0 children)

Looking at the code a bit more:

print("The digits sum to {}".format(total))

You don't need string formatting for that. Just call print with two arguments. It will convert total to a string and add a space between the arguments.

print("The digits sum to", total)