TypeError:使用模运算符时,不是在字符串格式化期间转换的所有参数

时间:2017-11-19 17:44:11

标签: python

我试图在这个函数中使用模运算符来打印列表中最高的偶数,但我总是得到这个错误:

TypeError: not all arguments converted during string formatting

代码:

def largest_even():
    """
    -------------------------------------------------------
    Returns the largest even number.
    Use: result = largest_even()
    -------------------------------------------------------
    Preconditions:

    Postconditions:
        returns
        result - largest even number
    -------------------------------------------------------
    """
    num = int(input('Enter a number:'))
    n_list = []
    while num != "":
        n_list.append(num)
        num = input('Next number:')
    print(n_list)
    n = 0
    e_list = []
    while n < len(n_list):
        a = (n_list[n]) % 2
        if a == 0:
            e_list.append(n_list[n])
        n = n + 1
    print(n)
    print(e_list)
    if len(e_list) == 0:
        result = 'False'
    result = e_list[0]
    n = 0
    while n < len(e_list):
        if e_list[n] > result:
            result = e_list[n]
    print("{} is the largest even number in that list.".format(result))

1 个答案:

答案 0 :(得分:0)

您只需在列表中添加一个号码;在循环中,你正在添加字符串。

num = input('Enter a number:')
n_list = []
while num != "":
    n_list.append(int(num))
    num = input('Next number:')