-3
def validate(bpp):

    for i in range(bpp):
        if bpp < 3:
            raise ValueError("Please have 3 values in the list.")        
    if bpp[0] != "add":
        raise ValueError("Please use 'add','div','sub' or 'mul' along with two numbers.")
    if bpp[0] != "div":
        raise ValueError("Please use 'add','div','sub' or 'mul' along with two numbers.")
    if bpp[0] != "mul":
        raise ValueError("Please use 'add','div','sub' or 'mul' along with two numbers.")
    if bpp[0] != "sub":
        raise ValueError("Please use 'add','div','sub' or 'mul' along with two numbers.")

    if bpp[1] != type(int):
        raise ValueError("Please put an integer for your first operand.")
    if bpp[2] != type(int):
        raise ValueError("Please put an integer for your second operand.")           
    else:
        print("Everything is ready to go! Now go evaluate your numbers!")

It's giving me 'str' object cannot be interpreted as an integer for the "for i in range(bpp). I'm trying to get it to see that if there are less than 3 variables in the list bpp, then it will give a ValueError. So something like

validate(["add", 10, 10"])

and it gives me back 20

Brady L.
  • 23
  • 3
  • 2
    Does this answer your question? [How do I get the number of elements in a list?](https://stackoverflow.com/questions/1712227/how-do-i-get-the-number-of-elements-in-a-list) – buran Dec 03 '21 at 07:44
  • `bpp` is a list. `range()` expects an _integer_. – Pranav Hosangadi Dec 03 '21 at 07:44
  • 2
    Note that your updated snippet has number of other issues, e.g. `bpp[1] != type(int)` will always be `True` (you are comparing int value with type `type`) – buran Dec 03 '21 at 07:44
  • Your code will not work as intended anyways, your ifs check if bpp[0] doesn't equal "add" and if not returns error, otherwise continue and returns error on second condition, because it cannot have multiple values, you probably want to check if it equals one of those and if not raise error, i would create a list of values and check if list contains value of bpp[0] – Ruli Dec 03 '21 at 07:48
  • Other issues: 1. You use a bunch of `if`s instead of `if..elif..elif`s, so your function will _always_ throw a `ValueError` because `bpp[0]` cannot be all of `"add"`, `"sub"`, etc. 2. `!= type(int)` isn't how you check if an object is an instance of a type. Not so much an issue: Unless you're handling the error somewhere, it's not the best idea to raise an error just to tell the user they've done something wrong. – Pranav Hosangadi Dec 03 '21 at 07:49
  • There are multiple problems in the given code, the simplified version should look like: `import operator #set this anywhere and easily extend your number of operators operators = ["add", "div", "mul", "sub"] #translation ops = { "add": operator.add, "sub": operator.sub. "mul": operator.mul, "div": operator.div} return ops[bpp[0]](bpp[1],bpp[2])` also here are some hints. Operator can be helpful. https://stackoverflow.com/questions/1740726/turn-string-into-operator – Ruli Dec 03 '21 at 08:10

2 Answers2

1

Since you use the elements differently, you could also use tuple unpacking to both check the length and put the values in separate variables:

op, first, second = bpp

The rest of the function can then use op, first and second rather than having to use the numbers, which will be easier to read.

Tuple unpacking will automatically give a ValueError if you have an unexpected number of items in the list bpp, with a built-in message; if you want to customise the message, you can catch it and raise a different one:

try:
  op, first, second = bpp
except ValueError:
  raise ValueError("Please have 3 values in the list.")
Jiří Baum
  • 6,697
  • 2
  • 17
  • 17
0

Use the len function to check the length of the list; it's not necessary to iterate over it with range:

    if len(bpp) < 3:
        raise ValueError("Please have 3 values in the list.")  

Since the rest of your validation function assumes exactly 3 values, you probably want to use != rather than <. I'd also suggest using a single check to validate the operator, and using isinstance instead of type(...) = ... (which I think you got backwards in your code).

def validate(bpp):
    if len(bpp) != 3:
        raise ValueError("Please have 3 values in the list.")
    if bpp[0] not in ("add", "div", "mul", "sub"):
        raise ValueError("Please use 'add','div','sub' or 'mul' along with two numbers.")
    if not isinstance(bpp[1], int):
        raise ValueError("Please put an integer for your first operand.")
    if not isinstance(bpp[2], int):
        raise ValueError("Please put an integer for your second operand.")
    print("Everything is ready to go! Now go evaluate your numbers!")
Samwise
  • 68,105
  • 3
  • 30
  • 44