1

I'm making an RPG and I'd like to call up a character sheet function. I've stored the stats in a list, and I'm trying to put them in the sheet:

global char
char = ["Name" , 100,100,30,30]

def sheet():
    global char
    print("Character sheet: \nName: {} \nHP: {}/{} \nMP: {}/{}".format(char[0:])

I expected it to come out like this:

Character sheet:
Name: Name
HP: 100/100
MP: 30/30

But it ends up with an error:

Tuple index out of range

I realize I could do it separately,

print("print("Charecter sheet: \nName: {} \nHP: {}/{} \nMp: {}/{}".format(char[0],char[1],...)

But I'd be interested in other ways to make my code look cleaner.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87

1 Answers1

1

The problem is that you are passing only one value to format (your list slice char[0:] which is the same as char) but want to format your output-string with 5 values. Unpack the values.

print("Character sheet: \nName: {} \nHP: {}/{} \nMP: {}/{}".format(*char))

Also, your last print statement lacks a closing parenthesis, I fixed that in my answer.

timgeb
  • 76,762
  • 20
  • 123
  • 145
  • @JohnZuniga np, happy coding. Please note the comments on your question regarding the use of `global`. – timgeb Jan 02 '16 at 14:07