for循环,元组超出范围

时间:2016-10-21 13:34:49

标签: python python-3.x indexing range

不太确定导致此错误的原因 在第5天使用Hackerrank 30天的挑战,我似乎无法改变这一点,以便它可以工作 - 我不太熟悉占位符,但对它们的工作方式有基本的了解

#!/bin/python3
import sys

n = int(input().strip())
for i in range(1, 10):
    answer = n * i
    print("{} x {} = {}".format((n, i, answer)))

错误:

Traceback (most recent call last):
  File "solution.py", line 9, in <module>
    print("{} x {} = {}".format((n, i, answer)))
IndexError: tuple index out of range

1 个答案:

答案 0 :(得分:1)

n = int(input().strip())
for i in range(1, 10):
    answer = n * i
    print("{} x {} = {}".format(n, i, answer))  # changed here 

您有一个传递给n,i,answer的{​​{1}}元组。您只需要将要打印的内容和格式传递给函数format(),而无需将其包装在元组中。

相关问题