问题描述
x = int(input())
n = [int(i) for i in input().split()]
middle = n[int((x - 1) / 2)
even = 0
odd = 0
for number in n:
if number % 2 == 0:
even += number
else:
odd += number
answer = even * odd + middle ** 2
print("{} x {} + {}^2 = {}".format(even,odd,middle,answer))
解决方法
由于n是用户输入的列表值的最小数目,因此会产生诸如IndexError: list index out of range
之类的错误。
由于您的意图是使n具有比用户输入的最小位数更多的值,所以我在列表迭代中添加了.range()
。
这是修改后的代码:
x = int(input("Put in a number: "))
n = [int(i) for i in range(int(input("Put in another number: ")))]
middle = n[int((x - 1) / 2)]
even = 0
odd = 0
for number in n:
if number % 2 == 0:
even += number
else:
odd += number
answer = even * odd + middle ** 2
print("{} x {} + {}^2 = {}".format(even,odd,middle,answer))
如果您有任何疑问或需要澄清,请随时询问。