0
While syntax error
I keep getting a syntax error on the while line in the code below. I can't work out what I'm doing wrong. Any help would be appreciated! odd_numbers = 0 even_numbers = 0 number=int(input("Enter a number or type 0 to end: ") while number !=0: if number %2==1: odd_numbers+=1 else: even_numbers+=1 print ("Odd numbers: ", odd_numbers) print ("Even numbers:", even_numbers)
4 ответов
+ 9
There are 2 issues:
(1) line 3 is a missing closing parenthesis at the end of the line
(2) while loop leads to an infinite loop.
+ 7
If you include the input() function inside the loop, you can get rid of the issue (2).
Here is the code:
odd_numbers = 0
even_numbers = 0
number = 1
while number !=0:
number=int(input("Enter a number or type 0 to end: "))
if number %2==1:
odd_numbers+=1
else:
even_numbers+=1
print ("Odd numbers: ", odd_numbers)
print ("Even numbers:", even_numbers)
+ 2
Brilliant! Thank you, Lothar!
+ 1
Great!