0

I am trying to write a code, which will print a value to a certain amount of time which is indicated by the rep.

ex.

def bla(value, rep):
   value*rep
bla('x', 2) # output: xx

the part which I do not know how to do is the function should ensure that the parameters given are valid.

I want the rep value to not operate if it is anything but an integer. example:

def bla(value, rep):
   print (value*rep)
bla ('a', hello)

"sorry 'hello' is not a valid parameter"

JeremyFromEarth
  • 14,344
  • 4
  • 33
  • 47
AEL
  • 45
  • 2
  • 5
  • 1
    Your first code sample doesn't look like Python code to me. There's no indentation and you're missing a colon at the end of the `def` line. Did you transcribe it from memory? Please provide code that exhibits your problem. – Kevin Jan 24 '14 at 16:36

3 Answers3

5
def bla(value, rep):
    try:
        print value*rep
    except TypeError:
        print "sorry '%s' is not a valid parameter" % rep

see "Ask forgiveness not permission" - explain

Community
  • 1
  • 1
Guy Gavriely
  • 11,228
  • 6
  • 27
  • 42
  • I like this solution better than Rhand's. [EAFP](http://docs.python.org/2/glossary.html#term-eafp) is very pythonic! That said, if you were doing a whole lot of stuff with `rep`, you may want to check to make sure it can do all those things first :) – Adam Smith Jan 24 '14 at 16:42
  • Agreed, I spend to much time on java these days. – Rhand Jan 24 '14 at 17:56
0
def bla(value, rep):
    if isinstance(value, int ):
        print(value*rep)
    else:
        print("sorry '" + rep "' is not a valid parameter")
Rhand
  • 901
  • 7
  • 20
  • The biggest reason to use `try/except` here instead of type checking is in case I had built a class specifically to handle cases like this and wanted to pass it in. `isinstance(rep,int)` would fail when I passed my `MultString` class, or etc. – Adam Smith Jan 24 '14 at 16:43
  • e.g. [this](http://codepad.org/af3cgJPv) – Adam Smith Jan 24 '14 at 16:52
-1

For type checking in Python you can use type() function http://docs.python.org/2/library/functions.html#type ig:

>>> a = 2
>>> type(a) is int
True
>>> type(a) is str
False
>>> a = '2'
>>> type(a) is str
True
>>> type(a) is int
False

And you can use it in this way:

def bla(value, rep):
   if type(rep) is int:
       print (value*rep)
   else: 
       print ("Bad parameter type")
mart
  • 224
  • 2
  • 13
  • This fails for any object where `str*obj` is valid though (e.g. any object with an `__index__` method). Use Duck Typing: it's Pythonic – Adam Smith Jan 24 '14 at 16:57
  • You say the if statment will fail? Why? – mart Jan 24 '14 at 17:17
  • consider [http://codepad.org/YlepsX6v](http://codepad.org/YlepsX6v), which makes a custom class `MultString` that overrides `__mul__` so `MultString*str` is a valid expression. Your code will not give the desired result here, because `type(MultString()) is int == False` and will print `"Bad parameter type" and return the function. – Adam Smith Jan 24 '14 at 18:46
  • I still don't understand your point here just because the original question points "want the rep value to not operate if it is anything but an integer", but it's ok if my answer is wrong please downvote it in order to give the community a correct valoration of this particular wrong answer. – mart Jan 24 '14 at 18:53