+ 1
Why doesn't this work?
tuple = ("Luis", "Heather", "Pedro", "Max") print("My friends are %s, %s and %s." % (tuple[0:2], tuple[3])) It sais there are not enough arguments for format string
3 ответов
+ 6
Because the slice you did:
tuple[0:1]
results in a tuple (which has two string elements).
And your formatted print statement expects 3 string parameters but you give only two (one tuple and one string).
Try unpacking the slice and it will work. Just an extra asterisk like this:
print("My friends are %s, %s and %s." % (*tuple[0:2], tuple[3]))
+ 5
(tuple[0:2], tuple[3]) will result in (('Luis', 'Heather'), 'Max') (two values), but you need ('Luis', 'Heather', 'Max') (three values). 'tuple' shouldn't be used as a variable name, by the way
- 1
In each assignment to the %s, you must send only one str, so the correct code is as follows:
tuple = ["Luis", "Heather", "Pedro", "Max"]
print('My friends are %s, %s, %s and %s.' % (tuple[0], tuple[1], tuple[2],tuple[3],))