While syntax error | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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)

26th May 2020, 10:44 AM
Carrie Quinlan
Carrie Quinlan - avatar
4 Answers
+ 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.
26th May 2020, 10:52 AM
Lothar
Lothar - avatar
+ 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)
26th May 2020, 11:21 AM
Lothar
Lothar - avatar
+ 2
Brilliant! Thank you, Lothar!
26th May 2020, 11:11 AM
Carrie Quinlan
Carrie Quinlan - avatar
+ 1
Great!
26th May 2020, 11:24 AM
Carrie Quinlan
Carrie Quinlan - avatar