0

I have a function called Lookup that runs some info through an API. If the user's input is invalid and Lookup can't process it, it returns None. How would I write something to the effect of:

if lookup returns none:
    return apology("invalid input")
Brandon Zemel
  • 87
  • 1
  • 1
  • 10

3 Answers3

2

there are various ways you could achieve this.

if not lookup():
    return apology("invalid input")

if lookup() is None:
    return apology("invalid input")
Vishal Singh
  • 6,014
  • 2
  • 17
  • 33
0

Nevermind, figured it out (of course like a minute after posting this). In case anyone else is curious because I couldn't find an answer:

if lookup(symbol) == None:
    return apology("invalid symbol")
Brandon Zemel
  • 87
  • 1
  • 1
  • 10
  • should be carefule about if lookup, if not lookup, lookup is None, lookup == None – Derek Eden Jun 30 '20 at 02:20
  • What's the danger there? – Brandon Zemel Jun 30 '20 at 02:26
  • https://stackoverflow.com/questions/3257919/what-is-the-difference-between-is-none-and-none ... I've ran into some odd issues before, not saying you will but just saying be aware – Derek Eden Jun 30 '20 at 02:30
  • Good to know! Appreciate the tip – Brandon Zemel Jun 30 '20 at 03:57
  • Never use `==` or `!=` to check if `None`. See PEP8 (https://pep8.org/#programming-recommendations and https://www.python.org/dev/peps/pep-0008/#programming-recommendations): *Comparisons to singletons like None should always be done with is or is not, never the equality operators.* – Thomas Sep 29 '21 at 12:12
0
if not lookup():
    return apology("invalid")

This should solve the problem.