-4

I'm trying to create a simple while loop but there is a problem:

import random

weekDays = ["Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday", "Sunday"]

n = 0
while n < len(weekDays):
    diceRoll = random.randint(4, 6)
    print("On {} perform {} reps per set".format(weekDays[n]))
    n = n + 1

The issue I do not get it:

print("On {} perform {} reps per set".format(weekDays[n])) IndexError:Replacement index 1 out of range for positional args tuple

Will be appreciate for any suggestions.

CodeMonkey
  • 22,825
  • 4
  • 35
  • 75
  • 2
    In accordance with [ask], please research your issue before posting here. This is a duplicate of [IndexError: Replacement index 1 out of range for positional args tuple](https://stackoverflow.com/questions/63655115/indexerror-replacement-index-1-out-of-range-for-positional-args-tuple) – esqew Feb 22 '22 at 16:13
  • 1
    How many placeholders do you have in the string? How many values are you giving to `format`? – DeepSpace Feb 22 '22 at 16:13
  • Why use a `while` loop at all? `for day in weekDays: print("...".format(day, diceRol))`. You aren't using `n` except to index the list, so you can get rid of it altogether. – chepner Feb 22 '22 at 16:14

2 Answers2

1

Your format call is missing an argument (diceRoll). It's easier to keep track of the values you're formatting if you use f-strings rather than format, since they go straight in the string:

import random

weekDays = ["Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday", "Sunday"]
for day in weekDays:
    diceRoll = random.randint(4, 6)
    print(f"On {day} perform {diceRoll} reps per set")
Samwise
  • 68,105
  • 3
  • 30
  • 44
0

There needs two arguments in print("On {} perform {} reps per set".format(weekDays[n])).

You could change it to print("On {} perform reps per set".format(weekDays[n])). It will be worked.

TAN
  • 43
  • 6