Whats wrong with this code? | SoloLearn: Learn to code for FREE!

+1

Whats wrong with this code?

nums = [0,2,3,4] msg = "Points :{0} {2} {3} {4}".format(nums[0],nums[1],nums[2],nums[3]) print(msg)

5/25/2020 7:58:20 PM

sofronis

3 Answers

New Answer

+5

The string formatting you use mentions the position {4} which is not provided in the .format method. Remember that Python counts from 0, so {4} means inserting the *fifth* element. And you only provide four in the .format method.

+5

"Points :{0} {2} {3} {4}" should be 'Points :{0} {1} {2} {3}" The placeholders "{}" forms a tuple. 4 placeholder results in a tuple of 4 elements with last valid index as 3. Your code is trying to access the tuple at index 4 which does not exist hence the error.

0

ok, i thought that i should put the numbers from the list inside the {}