-1

I am relatively new to python programming and have written this code to scrape images of website and save it on my computer, although this code seems to work but in the end I am getting a error

if len(nametemp)== 0 or nametemp== None:
TypeError: object of type 'NoneType' has no len()

can someone please tell me what I am doing wrong?

Majid Roustaei
  • 1,556
  • 1
  • 20
  • 39
Biryanii
  • 81
  • 7
  • 6
    Don't post code as images. Edit your question and put the code as text (with correct formatting). – Andrej Kesely Jun 02 '20 at 16:48
  • in you `if` statement, swap two expressions, so you check `nametemp== None` first. Python as well as many other programming languages evaluates expressions left to right. So it will attempt to evaluate `len(nametemp) == 0` before proceeding to `nametemp== None` . If `nametemp` is indeed None, as your error message indicates, it fails checking first expression. You can also rewrite your if statement as simple `if not nametemp`. Empty lists and None is falsy: https://stackoverflow.com/questions/39983695/what-is-truthy-and-falsy-how-is-it-different-from-true-and-false – Oleg Jun 02 '20 at 18:47

1 Answers1

1

Change the order in the if condition,

if nametemp == None or len(nametemp) == 0:
Priyank-py
  • 349
  • 3
  • 14
  • This works because if the first condition evaluates to True the second will not be evaluated at all. This makes it so `len` is only called if `nametemp` is not `None` – RedKnite Jun 02 '20 at 17:08
  • @DanishMallick remember to mark the answer if it was helpful, https://meta.stackexchange.com/a/5235 – 0m3r Jun 11 '20 at 18:56