All pastes #269063 Raw Edit

Something

public python v1 · immutable
·published 2006-12-06 06:49 UTC
rendered paste body
import string## this first function checks if it is an integer or notdef is_int(instr):    if instr[0] == '+' or instr[0] == '-':        pos = 1    else:        pos = 0    while pos < len (instr):        if instr[pos] not in string.digits:            return False # a non-digit invalidates it all        pos = pos + 1 # ok so far    return True		#passed all tests - it's an INT!!!## this function gets an integerdef get_int(msg, errmsg):    x = raw_input (msg) # first attempt    while len(x) == 0:                            #if they simply hit enter without inputting anything it will prompt them again until they do        print "You need to enter something."        x = raw_input(msg)    while not is_int (x): #it is an integer        print errmsg # no tell user        x = raw_input (msg) #and make them try again    return int(x)## this function gets a positive integerdef get_pos_int(msg, errmsg):    x = raw_input (msg) # first attempt    while len(x) == 0:                            #if they simply hit enter without inputting anything it will prompt them again until they do        print "You need to enter something."        x = raw_input(msg)    while (not is_int (x)) or int (x) < 0: #it is a positive integer        print errmsg # no tell user        x = raw_input (msg) #and make them try again    return int (x)##this function gets a floating pointdef get_float(msg, errmsg):    instr = raw_input(msg)    while len(instr) == 0:        print "You need to enter something."        instr = raw_input(msg)    if instr[0] == '+' or instr[0] == '-': #support for positive and negative        pos = 1    else:        pos = 0    while pos < len (instr):        if not (instr[pos] in string.digits or instr[pos] == '.'): # support for floating points            print errmsg            instr = raw_input (msg)        pos = pos + 1    return float(instr)## this function forces a single character string inputdef get_single(msg, errmsg):    x = raw_input(msg)    while len(x) == 0:        print "You need to enter something."        x = raw_input(msg)    while len (x) > 1:        print errmsg        x = raw_input(msg)    return str(x)## this function forces a 4 digit integer input and returns it as a stringdef get_four(msg, errmsg):    x = get_pos_int(msg, errmsg)    x = str(x)    while len(x) > 4:        x = get_pos_int(msg, errmsg)        x = str(x)## if the input began with a zero it will be three digits , so a zero is added infront here    if len(x) < 4:        x = '0' + x        return x    return x## this function gets input in the 24 hour time format (2453, 1901 etc)def get_time(msg, errmsg):## we start off by calling for a 4 digit input    x = get_four(msg, errmsg)## if the first digit of that string happens to be a zero we take this path    while int(x[0]) == 0:## two variables are set.  one for the hours, one for the minutes.        x1 = x[0] + x[1]        x2 = x[2] + x[3]## this loop is in case they enter 00 for hours, we are using 24 as the midnight hour and not 00 so we have to take care of this possibility        while int(x1) < 1:            print errmsg            x = get_four(msg, errmsg)            x1 = x[0] + x[1]            x2 = x[2] + x[3]## if the first digit is zero and also the third digit is zero we can return the value, because there is no possible wrong combination        if int(x[2]) == 0:            return x## this loop is incase they enter greater than 59 for the minutes, and it also takes care of that 00 problem mentioned above        while int(x2) > 59 or int(x1) < 1:            print errmsg            x = get_four(msg, errmsg)            x1 = x[0] + x[1]            x2 = x[2] + x[3]        return x## incase the first digit wasnt zero we set the hour and minute variables again here    x1 = x[0] + x[1]    x2 = x[2] + x[3]## this loop is incase the hours are greater than 24, or the minutes greater than 59, and it also has that 00 problem taken care of    while int(x1) > 24 or int(x1) < 1 or int(x2) > 59:        print errmsg        x = get_four(msg, errmsg)        x1 = x[0] + x[1]        x2 = x[2] + x[3]## this if is thrown in here too incase the new input begins with a 0        if int(x[0]) == 0:            x1 = x[0] + x[1]            x2 = x[2] + x[3]            if int(x[2]) == 0:                return x## the minutes again are verified here            while int(x2) > 59 or int(x1) < 1:                print errmsg                x = get_four(msg, errmsg)                x1 = x[0] + x[1]                x2 = x[2] + x[3]            return x    return x