0

Hey guys, new user here, just taking a course on python and theres a simple task I'm trying which im failing. I'm basically trying to get a program to validate user input based on rules that i define. Namely, num1 input and num2 input are integers which can not be below 0 or above 99. The input is taken from an html form which ive got. It's just the pyhton side of it I am having trouble with. Would anyone be able to help? This is what I have so far. Feel free to make fun of how badly written the code is, thanks!

num1 = input("Enter Number: ")
num2 = input("Enter Number: ")

if num1 < 0:
print '%i' %(num1)
print ' error 1 '
else:
print "%i" %(input)
print ' ok 1 '
if num1 > 99:
print ' error 2 '
else:
print ' ok 2 '

if num2 < 0:
print ' error 3 '
else:
print ' ok 3 '

2
Contributors
1
Reply
3
Views
9 Years
Discussion Span
Last Post by sneekula
0

Genarally this is done this way:

def get_int(low, high):
    """
    the function will loop until an integer number within the
    given low and high range has been entered
    """
    prompt = "Enter an integer number between %d and %d: " % (low, high)
    while True:
        try:
            n = int(raw_input(prompt))
            if low <= n <= high:
                return n
        except ValueError:
            print "Integer value in range required!"

# test the function
n = get_int(1, 50)
print n
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.