• Register
1 vote
3.6k views

Problem:

When  try to run the below code an error message always appears. 
The code is:
import pynput
from pynput.keyboard import Key, Listener
count = 0
keys = {}

def on_press(key):
    global keys, count

    keys.append(Key)
    count += 1
    print("({0} pressed".format(key))

if count >= 10:
    count = 0
    write_file(keys)
    keys={}
def write_file(keys):
  with open ("keyloger.txt","a")as f:
    for key in keys:
     f.write(str(key))

with Listener(on_press=on_press)as listener:
    listener.join()

The error message that appear is:

  File "Main-File.py", line 1, in <module>
    import pynput
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.8/site-packages/pynput/__init__.py", line 40, in <module>
    from . import keyboard
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.8/site-packages/pynput/keyboard/__init__.py", line 52, in <module>
    from ._xorg import KeyCode, Key, Controller, Listener
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.8/site-packages/pynput/keyboard/_xorg.py", line 30, in <module>
    import Xlib.display
ModuleNotFoundError: No module named 'Xlib'

Can anyone fix this poblem?

10 7 2
6,060 points

Please log in or register to answer this question.

2 Answers

1 vote

Answer:

The error message says that the dictionaries in Python do not provide an append operation. You can instead just assign new values to their respective keys in a dictionary 

mydict = {}
mydict['item'] = input_value

If you just want to append values as they're entered you could instead use a list.

mylist = []
mylist.append(input_value)

Hope this will help you to fix your problem. 

11 6 4
34,950 points
1 vote

Solution:

As the error message point out, dictionaries in Python do not given an append operation.

You can instead only employ new values to their respective keys in a dictionary.

mydict = {}
mydict['item'] = input_value

In case you're wanting to append values as they're entered you could instead employ a list.

mylist = []
mylist.append(input_value)

Your line user['areas'].append[temp] seems like it is trying to access a dictionary at the value of key 'areas', in ncase you instead employ a list you must be able to perform an append operation.

Employing a list instead:

user['areas'] = []

On that note, you might want to inspect out the probability of employing a defaultdict(list) for your issue. 

Either employ dict.setdefault() in case the key is not included yet to dictionary :

dict.setdefault(key,[]).append(value)

or use, in case you meanwhile have the keys set up:

dict[key].append(value)

You have done everything right apart from where you have set

results = {} 

You were inferred to perform a list([ ]), however instead you have done a dictionary({ }). They are actually confusing, however your code must be:

def get_class_average(students):
    results = []
    for student in students:
        results.append(get_average(student))
    return average(results)

 

10 6 4
31,120 points

Related questions

0 votes
1 answer 20 views
20 views
Problem: Why I got this : Dict' object has no attribute 'append' python.
asked Mar 6 Wafa Abu Yousef 6.1k points
0 votes
1 answer 43 views
43 views
Problem: I am creating a loop in order to append continuously values from user input to a dictionary but i am getting this error: AttributeError: 'dict' object has no attribute 'append'
asked Feb 14 Mashhoodch 13k points
1 vote
1 answer 128 views
128 views
Problem: I am having a list of four values assuming clock time. My goal is to print them as: ['9AM', '10AM', '11AM', '12PM'] end of my program. According to my thoughts, I come up with some codes. That looks like exactly as shown below: Type: <class ' ... attribute 'append&rsquo;" every time I try to run the program. Could you, please help me to fix the issues that stopping me? Thanks in advance.
asked May 31, 2020 adamSw 11.3k points
0 votes
1 answer 42 views
42 views
Problem: Hello! I am a new programmer and I recently learned the concept of container types in Python. I have never worked with dictionaries and lists before. I tried running the following program in which I am adding the items into a dictionary through a list: lst = ... but I am unable to understand why it is showing an error with the dictionary. Could someone please help me sort this error out?
asked Apr 6 Code Learner 9.9k points
0 votes
1 answer 128 views
128 views
Problem: At the bottom of the code you have to add these lines: simFlips(100,100) show() Here is the error that I get when I run it on ubuntu: Traceback (most recent call last): File "coin.py", line 36, in <module> simFlips(100,100) File "coin.py", ... (heads - tails)) AttributeError: 'numpy.ndarray' object has no attribute 'append' Please tell me what I'm doing wrong that gives me the last error
asked Jan 11 Mashhoodch 13k points
1 vote
1 answer 374 views
374 views
Problem: Hello good people, I Just recently shifted to python3 from python2. I am working with a dict in my program. Certainly, my program has returned an error, 'dict' object has no attribute 'has_key' Here is the sample snippet of my script >>> d = {1:'a', ... guys? Do you have any idea about this? I am looking forward to having a solution to this error. Thanks for your concern and helpful mind.
asked Apr 30, 2020 Gavin 15.3k points
0 votes
1 answer 88 views
88 views
Problem: The following error message is displayed: &ldquo;Oops, try again. get_class_average([alice]) resulted in an error: &lsquo;list&rsquo; object attribute &lsquo;append&rsquo; is read-only&rdquo; This is my code: lloyd = { "name": "Lloyd", "homework": [90.0, 97.0, 75.0, 92.0], "quizzes": [88.0, 40.0, 94.0], "tests": [75.0, 90.0] }
asked Feb 18 Mashhoodch 13k points
0 votes
1 answer 468 views
468 views
This line: sift = cv2.xfeatures2d.SIFT_create() return error: Traceback (most recent call last): File "C:/Python27/openCVskrypty/GUI/SOLUTION2.py", line 11, in <module> sift = cv2.xfeatures2d.SIFT_create() AttributeError: 'module' object has no attribute 'xfeatures2d' I ... ) and I haven't got xfeatures2d module. Does anyone know why? Can I download it separately? Thanks for help how fix this.
asked Oct 24, 2020 psandprop 2.4k points
0 votes
1 answer 4 views
4 views
Problem: I have been trying to solve this problem, please help > Attributeerror: 'series' object has no attribute 'sort'
asked 6 days ago Junia Phoebe 23k points
0 votes
1 answer 28 views
28 views
Problem: Can anyone help me with this problem? > Attributeerror: 'tensor' object has no attribute 'numpy'
asked 6 days ago Junia Phoebe 23k points