2017-01-18 39 views
0

我很難讓程序不忽略第一個用戶輸入值。我想讓程序獲取該值,並且如果它的值爲偶數,則將其添加到其他值中,如果奇數值,則將其添加到其他值。此外,我試圖讓程序計算輸入了多少偶數和奇數,但第一個數字再次被忽略。程序似乎忽略了第一個用戶輸入

# 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

*零是作爲甚至

+2

看看您何時要求用戶輸入值。穿過你腦袋裏的循環。在收集另一個值之前,您是否使用您在循環之外收集的值? –

+1

'interger_int == 0'可能不會做你認爲它的工作,爲什麼你在檢查它是偶數還是奇數之前得到第二個輸入 – depperm

+0

你的'if ... else'沒有正確縮進 – depperm

回答

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)