Dylan
Dylan

Reputation: 21

Python enterInteger Function - check for valid integer

I've got an assignment for school (well, it's extra credit to be exact) where I need to use a function to check if a value that was entered is an integer. This is the function my teacher would like us to use to start with:

def enterInteger():
enteredValue = input(‘Please enter an Integer:’
return int(enteredValue)

I've tried messing with a bunch of different code but to no avail. This is the code I've had the most luck with (it's super sloppy, I know. I've been coding Python for about 3 weeks so far with not much teaching):

def enterInteger():
    check = 0
    enteredValue = input('Please enter an Integer: ')

    while check == 0:
        if "." in enteredValue:
            print("The entered value has a decimal! Not an integer!")
            return

        elif "\\" in enteredValue:
            print("The entered value is a fraction! Not an integer!")
            return

        elif "/" in enteredValue:
            print("The entered value is a fraction! Not an integer!")
            return
        else:
            return int(enteredValue)
            enteredValue = 0
            check = 1

check = enterInteger()

if type(check) is int:
    print("The entered value is an integer!")
else:
    enterInteger()

When I run this and enter a real integer, it works fine, but if I enter a non-integer, it doesn't enter the loop correctly. It also doesn't work if I enter a non-integer followed by an integer.

Any help I can get will be much appreciated!

Upvotes: 0

Views: 539

Answers (1)

Kevin
Kevin

Reputation: 76244

return causes the function to immediately terminate. You return in every branch of your if-elif-else block, so your loop can never execute more than once.

The most straightforward solution is to remove every return except return int(enteredValue). You'll also need to move the input call inside the loop since you want them to re-enter the value after a failure.

def enterInteger():
    check = 0

    while check == 0:
        enteredValue = input('Please enter an Integer: ')
        if "." in enteredValue:
            print("The entered value has a decimal! Not an integer!")
        elif "\\" in enteredValue:
            print("The entered value is a fraction! Not an integer!")

        elif "/" in enteredValue:
            print("The entered value is a fraction! Not an integer!")
        else:
            return int(enteredValue)
            enteredValue = 0
            check = 1

check = enterInteger()

if type(check) is int:
    print("The entered value is an integer!")
else:
    enterInteger()

A couple more improvements: there's not much point having code after a return, since it will never get executed, so you may as well delete enteredValue = 0 and check = 1. And since check will always be zero now, you may as well make the loop unconditional. And enterInteger always returns an int, so there's no point in checking that check is an integer.

def enterInteger():
    while True:
        enteredValue = input('Please enter an Integer: ')
        if "." in enteredValue:
            print("The entered value has a decimal! Not an integer!")
        elif "\\" in enteredValue:
            print("The entered value is a fraction! Not an integer!")
        elif "/" in enteredValue:
            print("The entered value is a fraction! Not an integer!")
        else:
            return int(enteredValue)

check = enterInteger()

This still doesn't satisfy the "it can not crash regardless of what is entered" requirement, though -- for instance, if the user enters "Hello", or hits ctrl-C. You may need to look into exception handling to cover those situations.

Upvotes: 1

Related Questions