def canBeInt(s): ''' s is a string. Return True if each char of all but the last char of s is in '0123456789', and the last character is also in that string or is '\n.' Otherwise, return False.''' numbers = "0123456789" returnVal = True # s[:-1] is s without the last character; we will cover this soon for c in s[:-1]: if not c in numbers: returnVal = False returnVal = returnVal and (s[-1] in numbers + "\n") return returnVal def main(): first = 10 last = 20 msg = "Enter an int between " + str(first) msg += " and " + str(last) + " " ans = input(msg) while not canBeInt(ans) or\ (int(ans) < first or int(ans) > last): if not canBeInt(ans): print("The value must be an int") else: print("The number must be between",first,"and",last) ans = input(msg) print("Finally, we have a valid answer:",ans) main()