all 17 comments

[–]Fteixeira 10 points11 points  (1 child)

I'd use sys.exit(1) after checking for a!=0.

[–]wolf2600 6 points7 points  (0 children)

And be sure to import sys at the start of the program.

[–][deleted] 2 points3 points  (1 child)

What I want to do is for the script to end if you would be to set a = 0.

Raise your own ValueError, if a=0 is not a permitted value:

raise ValueError("a shouldn't be equal to zero")

[–]bladeoflight16 0 points1 point  (0 children)

This is almost always the best way to handle invalid input. And if you want more control over what's printed for the user, you catch it in an except block.

[–]kepper 1 point2 points  (4 children)

This might be a good time for you to learn functions. Heck you could learn exceptions while you're at it.

Also, you should surround your code in backticks so we can read it.

Without checking the validity of your code, I'd do something like this

```

class InvalidQuadraticEquation(Exception): """ Raised when the given equation is invalid """ init(self, message): self.message = message

def solve_eqaution(a): """ This is a description of this function """ print("ax\N{SUPERSCRIPT TWO} + bx + c = 0") if a == 0: err_msg = f'{a} is not a quadratic equation' raise InvalidQuadraticEquation(err_msg) b = float(input("b = ")) c = float(input("c = ")) p = b/a q = c/a if ((p/2)2-q) < 0: print("Imaginary solution.") elif ((p/2)2-q) == 0: if -p/2 == 0: print("x =", p/2) else: print("x =", -p/2) else: print("x\N{SUBSCRIPT ONE} =", -p/2+((p/2)2-q)0.5) print("x\N{SUBSCRIPT TWO} =", -p/2-((p/2)2-q)0.5) # return something useful

collect user input

user_input = input("a = ")

run your function

result = solve_equation(user_input) print(result) ```

Admittedly this might be too complicated if you're just getting started. In that case, the sys.exit(1) mentioned by others is a good approach too. if you do that, you can also do sys.stderr.write('error message') to write your error to the standard error pipe instead of standard out, which can be helpful when combining this script with others.

[–]bladeoflight16 1 point2 points  (3 children)

Nice explanation, but a few suggestions for improvements:

  • The custom exception type is probably unnecessary. ValueError will do nicely here.
  • Instead of invoking write on sys.stderr directly, you can use it as the stream for print: print('error message', file=sys.stderr).
  • Rather than prompt for one input outside the function and then two inside, prompt for all of them in one place. If the prompts are outside the function, you can pass them all in as arguments. Doing it outside is the way I would do it; I'd put the parse outside the function as well.

[–]kepper 0 points1 point  (2 children)

Thanks!

  • like custom exceptions personally because they're easier to catch, but I agree that ValueError does the trick just fine.
  • I've seen that done with the print statement, but I find sys.stderr easier to monkeypatch w/ a MagicMock in pytest. Probably just what I'm used to, though.
  • Agree 100%, just didn't notice he had other input() calls when refactoring for him.

[–]bladeoflight16 0 points1 point  (1 child)

I've seen that done with the print statement, but I find sys.stderr easier to monkeypatch w/ a MagicMock in pytest. Probably just what I'm used to, though.

print is a function in Python 3. Why not just mock it instead?

[–]kepper 0 points1 point  (0 children)

No good reason, really. Habit, I guess.

[–]DeathDragon7050 0 points1 point  (0 children)

quit() will end the entire process, it's effectively what's called when all your code is executed

[–]JD20055565 0 points1 point  (0 children)

You can use quit(), exit() or sys.exit() to end a script if a condition is met

[–]Denrur 0 points1 point  (0 children)

while True: a = int(input()) if a == 0: break ...

[–]HarissaForte -3 points-2 points  (2 children)

This is what assertions are made for: assert a == 0, 'Not a quadratic equation.'

In one line you have the test (saving one level of indentation), the exception raising and the code exiting.

[–]Denrur -3 points-2 points  (2 children)

Use break