How to Solve Python AttributeError: ‘dict’ object has no attribute ‘item’

AttributeError: ' dict ' object has no attribute ' item '

This error means that python cannot find the attributes of the corresponding object, and the beginners don’t know enough about the function object, which leads to errors

Original code:

favorite_languages ​​= {
     ' jen ' : ' python ' ,
     ' sarah ' : ' c ' ,
     ' edward ' : ' ruby ' ,
     ' phil ' : ' python ' ,
    }
for name,language in favorite_languages.item():
     print (name.title()+ " 's favorite language is " +language.title()+ " . " )

The text editors we use generally use obvious colors as hints:

 

 Modified code:

favorite_languages ​​= {
     ' jen ' : ' python ' ,
     ' sarah ' : ' c ' ,
     ' edward ' : ' ruby ' ,
     ' phil ' : ' python ' ,
    }
for name,language in favorite_languages.items():
     print (name.title()+ " 's favorite language is " +language.title()+ " . " )

operation result:

1 Jen ' s favorite language is Python. 
2 Sarah ' s favorite language is C. 
3 Edward ' s favorite language is Ruby. 
4 Phil ' s favorite language is Python. 
5 Sarah ' s favorite language is C.

 Summarize the commonly used Python error types as follows:

  • ZeroDivisionError-divide (or modulo) zero (all data types) 
  • ValueError-Invalid parameter passed in 
  • AssertionError-assertion statement failed 
  • StopIteration-the iterator has no more values 
  • IndexError-there is no such index in the sequence (index) 
  • IndentationError-indentation error 
  • OSError-input/output operation failed 
  • ImportError——Failed to import module/object 
  • NameError-Object not declared/initialized (no attributes) 
  • AttributeError- indicating that the object does not have this attribute
  • GeneratorExit-an exception occurs in the generator to notify the exit 
  • TypeError-invalid operation on the type 
  • KeyboardInterrupt-user interrupt execution (usually input ^C) 
  • OverflowError-Numerical operation exceeds the maximum limit 
  • FloatingPointError-floating point calculation error 
  • BaseException-the base class of all exceptions 
  • SystemExit-interpreter requests to exit 
  • Exception-the base class for general errors 
  • StandardError-the base class for all built-in standard exceptions 
  • ArithmeticError-the base class for all numerical calculation errors 
  • EOFError-there is no built-in input, reaching the EOF mark 
  • EnvironmentError-the base class for operating system errors 
  • WindowsError-system call failed 
  • LookupError-the base class for invalid data query 
  • KeyError-there is no such key in the map 
  • MemoryError-memory overflow error (not fatal to the Python interpreter) 
  • UnboundLocalError-access to uninitialized local variables 
  • ReferenceError-Weak reference attempts to access objects that have been garbage collected 
  • RuntimeError-general runtime error 
  • NotImplementedError-method not yet implemented 
  • SyntaxError Python-syntax error 
  • TabError-Tab and space are mixed 
  • SystemError-general interpreter system error 
  • UnicodeError-Unicode related errors 
  • UnicodeDecodeError-Unicode decoding error 
  • UnicodeEncodeError-Unicode encoding error 
  • UnicodeTranslateError-Unicode conversion error

The following are warning types 

  • Warning-the base class of warnings 
  • DeprecationWarning-warning about deprecated features 
  • FutureWarning-a warning that the semantics of the structure will change in the future 
  • OverflowWarning-old warning about automatic promotion to long integer (long) 
  • PendingDeprecationWarning-warning about the feature will be deprecated 
  • RuntimeWarning-warning of suspicious runtime behavior 
  • SyntaxWarning-warning of suspicious syntax 
  • UserWarning-warning generated by user code

Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *