计算用户输入Python 2.7中的偶数/奇数

时间:2017-04-11 17:51:08

标签: python

我想计算输入中的偶数/奇数的数量但是当我为它制作公式时它会给我一个错误: 在我的情况下,它必须在代码中有一个循环和迭代,所以不能显着改变任何东西。

exceptions.TypeError: not all arguments converted during string formatting

代码:

count_odd = None
count_even = None

while True:
    numbers = raw_input("Enter a number: ")
    if numbers == "done": break

    try:
        num = int(numbers)
    except ValueError:
        print "Not a number"
        continue

for x in numbers:
        if x % 2: #error is here
            count_even+=1
        else:
            count_odd+=1

print("Number of even numbers :",count_even)
print("Number of odd numbers :",count_odd)

0 个答案:

没有答案