0

I'm writing a game that prompts the user to input the number of rows. The problem I'm having is how do I get the program to keep prompting the user until they enters a whole number. If the user enters a letter or a float such as 2.5, the int value will not work, and thus I cannot break out of the loop. The program crashes. The int is essential so that I can check the number. The input must be even, it must be greater then or equal to 4 and less then equal to 16. Thanks!

def row_getter()->int:

    while True: 
        rows=int(input('Please specify the number of rows:'))
        if (rows%2 == 0 and  rows>=4 and  rows<=16):
            return rows
            break 
totoro
  • 2,469
  • 2
  • 19
  • 23
  • 1
    Have you tried using a try-except around the int conversion? – OneCricketeer May 30 '16 at 20:12
  • In the tutorial, look at [the section about errors and exceptions](https://docs.python.org/3.5/tutorial/errors.html) — It is _exactly_ what you need… – gboffi May 30 '16 at 20:13

3 Answers3

2

You're on the right path, but you want to use a try/except block to try and convert the input to an integer. If it fails (or if the input is not in the given bounds), you want to continue and keep asking for input.

def row_getter()->int:

    while True:
        try:
            rows=int(input('Please specify the number of rows:'))
        except ValueError:
            continue
        else: # this runs when the input is successfully converted
            if (rows % 2 == 0 and >= 4 and rows <= 16):
                return rows
            # if the condition is not met, the function does not return and
            # so it continues the loop
Rushy Panchal
  • 16,979
  • 16
  • 61
  • 94
  • Thanks so much! I'll try this. – Smoothievan May 30 '16 at 20:20
  • The continue is it necessary? It's function is to return to the beginning of the while loop. But it seems that it can function without? I'm just being curious about this. – Smoothievan May 30 '16 at 20:25
  • 1
    @Smoothievan: You need to have _something_ in the `except` clause. You can change the `continue` to `pass` if you like, although I think `continue` is slightly clearer to read. You may want to print something in the `else` clause if that `if` test fails so the user knows the row number is invalid. – PM 2Ring May 30 '16 at 20:26
  • 1
    @Smoothievan with `continue` you can skip the `else` construct, with `pass` you will need it. – totoro May 30 '16 at 20:35
0

I guess the pythonic way to do it would be:

while True:
    try:
        rows = int(input('Please specify the number of rows:'))
    except ValueError:
        print("Oops! Not an int...")
        continue
    # now if you got here, rows is a proper int and can be used

This idiom is called Easier to Ask Forgiveness than Permission (EAFP).

pzelasko
  • 2,082
  • 1
  • 16
  • 24
0

Could also make isint helper function for reuse to avoid the try/except in main parts of code:

def isint(s):
    try:
        int(s)
        return True
    except ValueError:
        return False


def row_getter()->int:  

    while True:
        s = input('Please specify the number of rows:')
        if isint(s):
            rows = int(s)
            if (rows % 2 == 0 and rows >= 4 and rows <= 16):
                return rows
totoro
  • 2,469
  • 2
  • 19
  • 23