Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

In a function in Django, the user can send me a number or a string, and I want to know if I received a number or a String (Tip: The number will always be an integer between 1-6)

I want to know if it's possible to detect this and how (with an example), as the number or string I'm getting will tell me what to do next.

share|improve this question
    
How is the number sent? In a request? –  Martijn Pieters Nov 15 '13 at 14:30
    
Like this: def make_choice(request, option): –  Sascuash Nov 15 '13 at 14:33
    
Then option is always a string. –  Martijn Pieters Nov 15 '13 at 14:34
    
So, trying to convert it into an integer like int(option) will only work if it's a number originally. Am I wrong? –  Sascuash Nov 15 '13 at 14:38

3 Answers 3

up vote 3 down vote accepted

You can try to convert the string to a number using int(), catching the exception:

def isNum(data):
    try:
        int(data)
        return True
    except ValueError:
        return False

This returns True only if the string can be converted to an integer number.

share|improve this answer
3  
@Christian: That is what exceptions are for. Not just to detect coding errors. And no, this is not slow, this is actually faster than the alternatives, which is to use a regular expression or a series of string method tests. –  Martijn Pieters Nov 15 '13 at 14:33
    
@Christian: Can you show me anything other than "It's probably slow"? Because that's not much of an argument; do you have proof? –  Martijn Pieters Nov 15 '13 at 14:44

What about: if isinstance(data, int):

share|improve this answer

I'm assuming your number will still be encased in a string, ie. "1" or "4" or "6" - if that's the case, then there are several ways to do it; you could use a regex to check whether it is a number or not, or you could knock up a function that would look something like this

def isNumber(your_input_string):
    return len(your_input_string) == 1 and your_input_string in "123456"

Since the number will always be between 1 and 6, it can only be a string of length 1, and it must be contained in the string '123456', since... well, those are the allowed numbers.

EDIT:

As pointed by Martijn Pieters in the comments below, that is a roundabout way of doing it; an easier solution would be just to check whether the string is between '1' or '6'.

def isNumber(your_input_string):
    return len(your_input_string) == 1 and '1' <= your_input_string <= '6'
share|improve this answer
2  
You can simply write that as return len(your_input_string) == 1 and your_input_string in "123456" and there are no true and false in python –  thefourtheye Nov 15 '13 at 14:41
2  
This is rather... round-about. return len(your_input_string) == 1 and '1' <= your_input_string <= '6' would do the same, but faster. –  Martijn Pieters Nov 15 '13 at 14:41
    
- I can't believe I overlooked the fact that I'm basically just returning a boolean expression. That's a bit embarassing. It's been a while since I last programmed anything in python, so I wasn't even sure if my syntax was correct. I didn't even remember that you could do comparisons with <= on strings - that's brilliantly elegant solution. –  Tobias Roland Nov 15 '13 at 14:46
    
You do need to test for the length here; '1' <= '16' <= '6' is True as well, due to lexicographic ordering. –  Martijn Pieters Nov 15 '13 at 15:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.