您的代码有一些问题,但它们很小:
1)您要求在主循环之前输入数字,因此输入的第一个整数不会求和(第1行和第2行)
2)对于你来说,在主循环中有一个for循环是没有意义的。你要做的是检查字符串中的每个字符。只是不是你想要的。
3)要忽略负数,只需检查负数是否小于0,如果小于0,则继续(中断循环)。
4)您使用了3个空格的缩进。这可能是文本编辑器的错误,所以请尝试将其配置为使用4个空格,这是Python中的标准。
5)公约规定,经营者周围应有空间。
6)正整数计数只是另一个简单的计数器。
所有的修改,这就是你的代码应该是什么样子:num_int = None
even_count = 0
odd_count = 0
even_sum = 0
odd_sum = 0
while num_int != 0:
num_str = input("Input an integer (0 terminates):")
num_int = int(num_str)
if num_int < 0:
continue # Continue the loop and go back to asking input
# If the loop reaches this point we know it's a positive number, so just add one
positive_count += 1
if num_int % 2 == 0:
even_count += 1
even_sum += num_int
else:
odd_count +=1
odd_sum += num_int
print("")
print("Sum of odds:", odd_sum)
print("Sum of evens:", even_sum)
print("Even count:", even_count)
print("Odd count:", odd_count)
print("Total positive int count:", positive_count)