0

I am new to python and I am taking a summer online class to learn python.

Unfortunately, our professor doesn't really do much. We had an assignment that wanted us to make a program (python) which asks the user for a number and it determines whether that number is even or odd. The program needs to keep asking the user for the input until the user hit zero. Well, I actually turned in a code that doesn't work and I got a 100% on my assignment. Needless to say our professor is lazy and really doesn't help much. For my own knowledge I want to know the correct way to do this!!! Here is what I have/had. I am so embarrassed because I know if probably very easy!

counter = 1
num = 1
while num != 0:
counter = counter + 1
   num=int(input("Enter number:"))
   while num % 2 == 0:
   print ("Even", num)
else:
    print ("Odd", num)
1
  • 1
    pls make sure to accurately represent your indentation Commented Jul 25, 2017 at 0:18

4 Answers 4

2

There are a couple of problems with your code:

  • You never use counter, even though you defined it.
  • You have an unnecessary nested while loop. If the number the user inputs is even, then you're code will continue to run the while loop forever.
  • You are incorrectly using the else clause that is available with while loops. See this post for more details.

Here is the corrected code:

while True:
    # Get a number from the user.
    number = int(input('enter a number: '))

    # If the number is zero, then break from the while loop
    # so the program can end.
    if number == 0:
        break

    # Test if the number given is even. If so, let the
    # user know the number was even.
    if number % 2 == 0:
        print('The number', number, 'is even')
    # Otherwise, we know the number is odd. Let the user know this.
    else:
        print('The number', number, 'is odd')

Note that I opted above to use an infinite loop, test if the user input is zero inside of the loop, and then break, rather than testing for this condition in the loop head. In my opinion this is cleaner, but both are functionally equivalent.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for explaining what I did wrong. I really appreciate your help! Thank You, again
Glad I could help out @JBarela :-)
1

You already have the part that continues until the user quits with a 0 entry. Inside that loop, all you need is a simple if:

while num != 0:
    num=int(input("Enter number:"))
    if num % 2 == 0:
        print ("Even", num)
    else:
        print ("Odd", num)

I left out the counter increment; I'm not sure why that's in the program, since you never use it.

Comments

0

Use input() and If its only number specific input you can use int(input()) or use an If/else statement to check

Comments

0

Your code wasn't indented and you need to use if condition with else and not while

counter = 1
num = 1
while num != 0:
    counter = counter + 1
    num = int(input("Enter number:"))
    if num % 2 == 0:
       print ("Even", num)
    else:
       print ("Odd", num)

Sample Run

Enter number:1
Odd 1
Enter number:2
Even 2
Enter number:3
Odd 3
Enter number:4
Even 4
Enter number:5
Odd 5
Enter number:6
Even 6
Enter number:0
Even 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.