Marshall K
Marshall K

Reputation: 333

Why does this program print the message more than once?

I'm new to programming and made a simple program to determine if a user inputted number was even or odd. I also made a function that checks if the inputted number is an integer rather than a float.

The program mostly executes correctly if a user types a number, but the message telling whether the number is odd or even prints multiple times if the user inputs multiple floats first. Why does that happen?

def check_int(x,y):
    if x != int(x):
        print "The number is not an integer"
        y()
    else:
        print "The number is an integer"

def even_odd():
    given_number = input("Please type an integer: ")
    check_int(given_number, even_odd)
    if (given_number % 2) != 0:
        print "The number is odd"
    elif (given_number % 2) == 0:
        print "The number is even"

even_odd() 

Upvotes: 0

Views: 115

Answers (3)

blank_name_here
blank_name_here

Reputation: 11

There's a couple of issues with your code.

First of all, you don't need an elif. If given_number % 2 !=0 is false then the opposite condition will always be True. So the opposite of != 0 is 0.

Also, don't pass a function as a callback. The way you wrote it could possibly crash your program. I suggest you read more about loops.

cheers!

Upvotes: 0

mkrieger1
mkrieger1

Reputation: 23142

even_odd is called from within check_int again (under the name y). That's why you see its output multiple times.

I suppose you think this is required in order to continue with even_odd after check_int is finished. This is not necessary. When a function is finished, the program automatically continues from where the function was called.

Simply remove the y parameter from check_int, remove the line y() and change check_int(given_number, even_odd) to check_int(given_number).

Upvotes: 2

Prune
Prune

Reputation: 77837

The problem is that you've used an arcane version of recursion to loop through your program. When you receive a float, you call check_int, print the message, and then recur to the input function. This leaves a hanging return to your first call on the stack. When you finally get an integer, you leave check_int normally, but then return to even_odd, which is still waiting to process the parity of the floats. That's what produces the extra lines of output.

When you reject something as a non-integer, you need to loop back to the input, or otherwise avoid checking the parity later.

def check_int(x,y):
    return x != int(x)

def even_odd():
    given_number = input("Please type an integer: ")
    if check_int(given_number, even_odd):
        print "The number is not an integer"
    else:
        print "The number is an integer"
        # Check parity
        if (given_number % 2) != 0:
            print "The number is odd"
        elif (given_number % 2) == 0:
            print "The number is even"

even_odd()

If you want to loop until you get an integer, write that as a separate, outer loop.

Upvotes: 2

Related Questions