Python Forum
IndexError: Replacement index 2 out of range for positional args tuple - help?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IndexError: Replacement index 2 out of range for positional args tuple - help?
#1
Exclamation 
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.
Reply
#2
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)))
Reply
#3
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  replacement for fpformat.extract GbSig1998 4 254 Apr-12-2024, 06:15 PM
Last Post: deanhystad
  IndexError: index 10 is out of bounds for axis 0 with size 10 Mehboob 11 2,119 Sep-14-2023, 06:54 AM
Last Post: Mehboob
Thumbs Down I hate "List index out of range" Melen 20 3,348 May-14-2023, 06:43 AM
Last Post: deanhystad
Question How to compare two parameters in a function that has *args? Milan 4 1,280 Mar-26-2023, 07:43 PM
Last Post: Milan
  String replacement in DB WJSwan 0 758 Dec-28-2022, 05:31 AM
Last Post: WJSwan
  IndexError: list index out of range dolac 4 1,924 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 2,359 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 1,448 May-03-2022, 01:39 PM
Last Post: Anldra12
  matplotlib x axis range goes over the set range Pedroski55 5 3,228 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  IndexError: list index out of range rf_kartal 6 2,862 Sep-07-2021, 02:36 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020