I am trying to make a program that will continuously take a user input and determine if the user input is even or odd. The program will only stop when zero is entered.
However, when I enter zero, it print
s 0 is an even number
followed by All done!
. The expected output is just All done!
. Here is my code:
def main():
total = 0
count = 0
while True:
entry=int(input('Enter a number or 0 to quit:'))
if entry % 2 == 0:
print (format (entry), 'is an even number.')
else:
print (format (entry),'is an odd number.')
if entry == 0:
print ('All done!')
break
main()
If you want to check whether the input is zero before doing anything else, do so. If you want to stop the loop in certain conditions, put the break
inside a conditional. Note that count
and total
were unused. Also, format()
with no formatting is redundant.
def main():
while True:
entry = int(input('Enter a number or 0 to quit:'))
if entry == 0:
print('All done!')
break
if entry % 2 == 0:
print(entry, 'is an even number.')
else:
print(entry, 'is an odd number.')
main()