0

Here's my code:

class Node:
    def __init__(self, cargo=None, next=None):
        self.cargo = cargo
        self.next = next


class LinkedList:
    def __init__(self):
        self.first = None
        self.last = None

# then I declare a list and a node
S = LinkedList()
cel = Node
cel = S.first

Now I want to put something in the list:

  n = 0
  x = 0
  while n < 5:
      x = input()
      cel.val = x
      cel = cel.next

Yet I get an error stating that:

 'NoneType' object has no attribute 'val'
 'NoneType' object has no attribute 'next'

Where is the problem?

1
  • You didn't state any variable called "val" nor stated any variable/integer to next so it will return None. Commented Nov 11, 2016 at 16:42

1 Answer 1

1

cel is equal to S.first. S.first is equal to None. When you try to get val from celm you try to get val attribute of None.

And none of you classes has val attribute... So it is possible to assign it, but I'd suggest to avoid it, because it is not clear to create it somewhere without declaring it in class itself.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.