Python Forum

Full Version: IndexError: Replacement index 2 out of range for positional args tuple - help?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello.

I've been trying for days to fix this error, but to no avail.

This is the code:

def secret_formula(started):
    jelly_beans = started * 500
    jars = jelly_beans / 1000
    crates = jars / 100
    return jelly_beans, crates, jars

start_point = 10000

jelly_beans, jars, crates = secret_formula(start_point)

print("With a starting point of: {}".format(start_point))
print("We'd have %d jelly beans, %d jars, and %d crates." % (jelly_beans, jars, crates))

start_point = start_point / 10

print("We can also do that this way:")
print("We'd have {0} jelly beans, {2} jars, and {1} creates.".format(secret_formula(start_point)))
It is throwing the error (IndexError: Replacement index 2 out of range for positional args tuple) when print("We'd have {0} jelly beans, {2} jars, and {1} creates.".format(secret_formula(start_point))) is called.

Any help would be greatly appreciated.

Thanks.
Adding a * will unpack the tuple returned from secret_formula
print("We'd have {0} jelly beans, {2} jars, and {1} creates.".format(*secret_formula(start_point)))
(Oct-14-2022, 09:53 PM)Yoriz Wrote: [ -> ]Adding a * will unpack the tuple returned from secret_formula
print("We'd have {0} jelly beans, {2} jars, and {1} creates.".format(*secret_formula(start_point)))

You're a genius! Thanks so much!