vubbz
vubbz

Reputation: 21

Python Noob: Input returning as none

Im still very new when it comes to python so be easy on me. Whenever I test this code it comes back with "None" instead of the input entered. Any idea why it could be happening?

def inputLandValue():
    while(1):
        try:
            value=int(input('Please enter the value of the property '))
            break
        except:
            print('Please enter a whole number (10000)')
            return value
def main():
    while(1):
        landValue = inputLandValue()
        print(landValue)
        doMoreStuff = input('Do you want to continue? y/n ')
        if(doMoreStuff.lower() != 'y'):
            break
main()
input() 

Upvotes: 0

Views: 66

Answers (2)

Bill Wang
Bill Wang

Reputation: 140

You can fix your problem by just putting 'return value' in place of the break in main().

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1121416

You indented your return value line too far. It is part of the except: handler, so it is only executed when you have no value! It should be outside the while loop:

def inputLandValue():
    while(1):
        try:
            value=int(input('Please enter the value of the property '))
            break
        except:
            print('Please enter a whole number (10000)')
    return value

or replace the break with return value:

def inputLandValue():
    while(1):
        try:
            value=int(input('Please enter the value of the property '))
            return value
        except:
            print('Please enter a whole number (10000)')

You should really only catch ValueError however; this isn't Pokemon, don't try to catch'm all:

except ValueError:

Upvotes: 5

Related Questions