-3
def get_integer(maximum):
x= int(input())
while x > 0 and maximum > x:
    if type(x) == type(5):
        return x

Inputs that can be interpreted as ints between 0 and the argument(maximum), if any input isn't within range or isn't a digit it loops back.

This is what I have so far, it runs, but when given multiple inputs I get-
ValueError: invalid literal for int() with base 10.

martineau
  • 119,623
  • 25
  • 170
  • 301
timrbUD
  • 3
  • 1
  • See: [python-error-handling-using-integers-as-input](https://stackoverflow.com/questions/11432913/python-error-handling-using-integers-as-input) -- you need to handle errors if someone input "fifehundredandten" or "4 9 2 83 2" – Patrick Artner Nov 12 '17 at 23:10
  • 1. fix intendation. 2. what you exaclty want? while loop will be endless because you dont do anything with x – domandinho Nov 12 '17 at 23:11
  • It doesn't make sense to have a `while` loop for something that cannot change. Use `if`. And if you want multiple inputs you'll need to `split()` the string based on how you expect the user to make multiple inputs. – roganjosh Nov 12 '17 at 23:12
  • please add expected output for user input examples to determine what you exactly want to implement – domandinho Nov 12 '17 at 23:13
  • 1
    This code makes little sense. First it tries to convert the string into an int, with no error checking. Then it enters an utterly pointless loop, for as long as the number is in range. And each time through the loop, it checks to see if the number, which already has to be an int, is still an int. – Tom Karzes Nov 12 '17 at 23:14

1 Answers1

0

Assuming the argument maximum is meaningful, as integers in python have no size limit, and no input text is wanted (e.g it says "Enter text here")

def get_integer(maximum):
    try:
        inp = int(input())
    except TypeError:
        print("Please input an integer")
        return get_integer(maximum)
    except ValueError:
        print("Please only enter numeric characters and no spaces")
        return get_integer(maximum)
    if inp >= 0 and inp <= maximum:
        return inp
    else:
        print ("Please input a number between 0 and %d", maximum)
        return get_integer(maximum)
John
  • 598
  • 2
  • 7
  • 22