程序似乎忽略了第一个用户输入

时间:2017-01-18 18:10:55

标签: python

我无法让程序忽略第一个用户输入值。我想让程序获取该值,并将其添加到其他偶数值(如果是偶数)或奇数值(如果它是奇数)。此外,我正在尝试让程序计算输入了多少偶数和奇数,但第一个数字再次被忽略。

# list of all the variables used in the program
interger_str = input("Input an interger (0 terminates): ")
interger_int = int(interger_str)
odd_count = 0
even_count = 0
even_sum = 0
odd_sum = 0

while (interger_int > 0 or interger_int < 0):
    interger_str = input("Input an interger (0 terminates): ")
    interger_int = int(interger_str)

    if (interger_int < 0):
        interger_int == 0
        interger_str = input("Input an interger (0 terminates): ")
        interger_int = int(interger_str)

# count the number of even intergers entered
    if (interger_int % 2 == 0):
        even_count
        even_count += 1
        even_sum += interger_int

# count the number of odd intergers entered
    if (interger_int % 2 == 1):
        odd_count
        odd_count = odd_count + 1
        odd_sum = odd_sum + interger_int
else:

    print("\nCount of odd numbers: ", odd_count)
    print("Count of even numbers: ", even_count)
    print("Sum of odd numbers: ", odd_sum)
    print("Sum of even numbers: ", even_sum)

提前致谢!

示例输出: Example

*零甚至包含在

1 个答案:

答案 0 :(得分:1)

您正在替换while循环中的第一个值。

下面的代码定义默认值,并在while循环中获取输入。

# list of all the variables used in the program
interger_int = 1
odd_count = 0
even_count = 0
even_sum = 0
odd_sum = 0

while (interger_int > 0 or interger_int < 0):
    interger_str = input("Input an interger (0 terminates): ")
    interger_int = int(interger_str)

    if (interger_int < 0):
        continue

# count the number of even intergers entered
    if (interger_int % 2 == 0):
        even_count
        even_count += 1
        even_sum += interger_int

# count the number of odd intergers entered
    elif (interger_int % 2 == 1):
        odd_count
        odd_count = odd_count + 1
        odd_sum = odd_sum + interger_int
    else:
        break

print("\nCount of odd numbers: ", odd_count)
print("Count of even numbers: ", even_count)
print("Sum of odd numbers: ", odd_sum)
print("Sum of even numbers: ", even_sum)