#Need help with wordy

5 messages · Page 1 of 1 (latest)

shut current
#

I have no idea what to do from here:(. Pls help, thank you!


special_char = string.punctuation.replace("-", "")
equations = ["plus", "minus", "divided", "multiplied"]

def answer(question):
    translator = str.maketrans("", "", special_char)
    question = question.translate(translator)
    question = question.lower().split()

    print(question)

    full_equations = []
    
    num_counter = 0
    for i in question:
        if i.strip("-").isnumeric():
            if question.index(i) < len(question) - 1:
                if question[question.index(i) + 1] not in equations:
                    raise ValueError("unknown operation")
            i = int(i)
            num_counter += 1
            full_equations.append(i)
        elif i in equations:
            full_equations.append(i)
    
    if num_counter == 0:
        raise ValueError("unknown operation")

    equation_flow = full_equations[0]
    for i in range(1, len(full_equations), 2):  # Step by 2 to get operators
        operator = full_equations[i]
        next_number = full_equations[i + 1]

        if operator == "plus":
            equation_flow += next_number
        elif operator == "multiplied":
            equation_flow *= next_number
        elif operator == "minus":
            equation_flow -= next_number
        elif operator == "divided":
            equation_flow //= next_number  # Using integer division

    return equation_flow

print(answer("What is 52"))```
ashen hound
#

What tests fail? What do they say?
Please share with a codeblock. Please do not use images. Expand tests to get details.

shut current
#

I redid the code a little, and here are the tests i failed


special_char = string.punctuation.replace("-", "")
equations = ["plus", "minus", "divided", "multiplied"]

def answer(question):
    translator = str.maketrans("", "", special_char)
    question = question.translate(translator)
    question = question.lower().split()

    print(question)

    full_equations = []
    
    num_counter = 0
    for i in question:
        if i.strip("-").isnumeric():
            if question.index(i) < len(question) - 1:
                if question[question.index(i) + 1] not in equations:
                    raise ValueError("unknown operation")
            i = int(i)
            num_counter += 1
            full_equations.append(i)
        elif i in equations:
            full_equations.append(i)
    
    if num_counter == 0:
        raise ValueError("unknown operation")

    equation_flow = full_equations[0]
    for i in range(1, len(full_equations), 2):  # Step by 2 to get operators
        try:
            operator = full_equations[i]
            next_number = full_equations[i + 1]
    
            if operator == "plus":
                equation_flow += next_number
            elif operator == "multiplied":
                equation_flow *= next_number
            elif operator == "minus":
                equation_flow -= next_number
            elif operator == "divided":
                equation_flow //= next_number  # Using integer division
        except (IndexError, ValueError) as exc:
                raise ValueError("syntax error") from exc

    return equation_flow

print(answer("What is 52"))```

1:
```with self.assertRaises(ValueError) as err:
    answer("What is?")
self.assertEqual(type(err.exception), ValueError)
self.assertEqual(err.exception.args[0], "syntax error")

Test Failure

AssertionError: 'unknown operation' != 'syntax error'
- unknown operation
+ syntax error```

2: 
```with self.assertRaises(ValueError) as err:
    answer("What is 1 plus plus 2?")
self.assertEqual(type(err.exception), ValueError)
self.assertEqual(err.exception.args[0], "syntax error")

Test Failure

TypeError: unsupported operand type(s) for +=: 'int' and 'str'```

3: ```with self.assertRaises(ValueError) as err:
    answer("What is 1 plus 2 1?")
self.assertEqual(type(err.exception), ValueError)
self.assertEqual(err.exception.args[0], "syntax error")

Test Failure

AssertionError: 'unknown operation' != 'syntax error'
- unknown operation
+ syntax error```
ashen hound
#

For 1 and 3 it looks like you're raising the wrong error type

uncut kernel
#

within triple backticks, specifying the language after the first one, it's better 4 the eyes 🙂

import string

special_char = string.punctuation.replace("-", "")
equations = ["plus", "minus", "divided", "multiplied"]

def answer(question):
    translator = str.maketrans("", "", special_char)
    question = question.translate(translator)
    question = question.lower().split()

    print(question)

    full_equations = []
    
    num_counter = 0
    for i in question:
        if i.strip("-").isnumeric():
            if question.index(i) < len(question) - 1:
                if question[question.index(i) + 1] not in equations:
                    raise ValueError("unknown operation")
            i = int(i)
            num_counter += 1
            full_equations.append(i)
        elif i in equations:
            full_equations.append(i)
    
    if num_counter == 0:
        raise ValueError("unknown operation")

    equation_flow = full_equations[0]
    for i in range(1, len(full_equations), 2):  # Step by 2 to get operators
        operator = full_equations[i]
        next_number = full_equations[i + 1]

        if operator == "plus":
            equation_flow += next_number
        elif operator == "multiplied":
            equation_flow *= next_number
        elif operator == "minus":
            equation_flow -= next_number
        elif operator == "divided":
            equation_flow //= next_number  # Using integer division

    return equation_flow

print(answer("What is 52"))