# 19/19 extra credit help

#1

Hi everyone,

So I’ve been focusing a little more on trying to debug my code when the user inputs something invalid for the game. I’ve found a solution but it’s a little ugly and inefficient. I was hoping for any tips that could give me the same result while being more efficient! Thanks. Here is my code:

#2

Plus, its making your code really inflexible. What if the board dimensions ever need to change?

I would personally pick something that hasn’t been taught yet, which is try/except:

``````try:
guess_row = int(raw_input("enter row: "))
except ValueError:
print "not a number"
``````

it will execute the except block when an error occurs.

#3

Think about numbers in a slightly different way
Instead of checking if a number is not 1 or 2 or 3 or 4 or 5

`(num < 1 || num > 5)`
accomplishes the same thing

Ideally you wouldn’t check against 1 and 5 but things like
max_row_size / max_col_size
min_row_size / min_col_size

#4

@fight_dragons, what if the user enters something that can’t be converted to integer? like a word or a letter from the alphabet?

#5
``if x not in range(1, 6):``

#6

`in` isn’t particular fast. Both is possible.

@fight_dragons, i think you mess up languages, the `or` operator in python isn’t `||`

#7

that’s O(1) in python3 or using xrange

anyway, yes, make a function to get input. use try/except in a loop, and keep asking until conversion succeeds and it’s within the range

#8

O(1)? Not O(N)? Doesn’t it depends on list length? The speed significance between `or` and `in` won’t be massive i suppose

#9

A C++ like solution
`while(std::cin.fail() || (num < min_row_size || num > max_row_size))`
I’m not sure about a python equivalent to it

#10

they’re not lists

what you’re dealing with is `range.__contains__` which will run something like `a < b < c`

#11

Awesome, still so much too learn it seems.

#12

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.