TypeError:不是在字符串格式化过程中转换的所有参数,Python

时间:2015-10-04 15:59:20

标签: python

我不知道为什么错误会出现。它是一个程序,用于显示包含7的数字,是7的倍数。

import sys
Numbers = raw_input()
Answer = 0
NumbersList = Numbers.split()
Length = len(NumbersList)
for num in range(0, Length):
    N = NumbersList [num-1]
    ListNumber = list(str(N))
    if (N == 0):
        print(Answer)
    elif (num == Length):
    print(Answer)
    elif (N % 7 == 0):
        Answer = Answer + 1
    elif 7 in ListNumber:
        Answer = Answer + 1


sys.stdout.flush()

新问题。我在底部的elif声明似乎不起作用。 输入是0-100的数字列表。停在0处。答案=数字%7 = 0或者数字为7

import sys
Numbers = raw_input()
Answer = 0
NumbersList = Numbers.split()
Length = len(NumbersList)
for num in range(0, Length):
    N = NumbersList [num]
    ListNumber = list(str(N))
    if (int(N) == 0):
        print(Answer)
        break
    elif (num == Length):
        if (int(N) % 7 == 0):
            Answer = Answer + 1
        elif 7 in ListNumber:
            Answer = Answer + 1
        print(Answer)
    elif (int(N) % 7 == 0):
        Answer = Answer + 1
    elif 7 in ListNumber:
        Answer = Answer + 1


sys.stdout.flush()

1 个答案:

答案 0 :(得分:0)

窃取@ PeterWood的评论,您需要先将输入转换为整数。

 N = int( NumbersList [num-1] )

您可能还需要考虑使用for num in NumberList和阅读https://www.python.org/dev/peps/pep-0008/来获取良好做法指南。

这对我有用(或没有错误):

import sys
Numbers = raw_input()
Answer = 0
NumbersList = Numbers.split()
Length = len(NumbersList)
for num in range(0, Length):
    N = int( NumbersList [num-1])
    ListNumber = list(str(N))
    if (N == 0):
        print(Answer)
    elif (num == Length):
        print(Answer)
    elif (N % 7 == 0):
        Answer = Answer + 1
    elif 7 in ListNumber:
        Answer = Answer + 1

print Answer
sys.stdout.flush()