Hello, I'm currently working on a school project in which i can not get my head around into figuring out how to make a loop until the user has input an interger this is what i have so far :

width = "hi"
length = "hello"
while width != int  and length != int:
    width = int(input("Please enter the width of your garden in meters "))
    length = int(input("Please enter the length of your garden in meters "))

obviously more needs to be added but the program still blows up every time a non integer value has been entered any ways on fixing it?

Look at this example, it should give you hints ...

''' input_integer1.py
loop until you get an integer input
'''

# make input() work with Python2 and Python3
try:
    input = raw_input
except:
    pass

def get_int():
    """this function will loop until an integer is entered"""
    while True:
        try:
            # return breaks out of the endless while loop
            return int(input("Enter an integer: "))
        except ValueError:
            print("Try again, value entered was not an integer.")

myinteger = get_int()
print("You entered %d" % myinteger)

Is there not a simpler way of doing this? Because i dont really recognise much of that code

You can ignore lines until 10 of Vegaseat's code if you are using Python 3, otherwise you can use raw_input instead of input on line 16. I would recommend this style though as I practise same style myself to get code function in both Python versions.

Since you are already using Python3, This would simplify it somewhat:

def get_int(prompt="Enter an integer: "):
    """this function will loop until an integer is entered"""
    while True:
        try:
            # return breaks out of the endless while loop
            return int(input(prompt))
        except ValueError:
            print("Try again, value entered was not an integer.")

width = get_int("Please enter the width of your garden in meters ")
length = get_int("Please enter the length of your garden in meters ")

# test
print(width, length)

Thanks for your help, is there anyway anyone could explain the code zzucker posted into further detail?

This question has already been answered. Start a new discussion instead.