Click here to Skip to main content
15,895,859 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
This is my current code, and im stuck. It should be at least 3 different variables from the original data. 


What I have tried:

<pre>class Node:
    def __init__(self, data=None, next=None):
        self.data = data
        self.next = next

#Making the data as linked list
class LinkedList:
    def __init__(self):
        self.head = None

    def insert(self, new_data):     # inserting the data at last
        newNode = Node(new_data)
        if (self.head):
            current = self.head
            while (current.next):
                current = current.next
            current.next = newNode
        else:
            self.head = newNode

    def push(self, new_data):      # inserting the data at first
        new_node = Node(new_data)
        new_node.next = self.head
        self.head = new_node

    def push_at(self, new_data, position):    # inserting the data at chosen position
        newNode = Node(new_data)
        if (position < 1):
            print("\nPosition should be >= 1.")
        elif (position == 1):
            newNode.next = self.head
            self.head = newNode
        else:
            temp = self.head
            for i in range(1, position - 1):
                if (temp != None):
                    temp = temp.next
            if (temp != None):
                newNode.next = temp.next
                temp.next = newNode
            else:
                print("\nThe previous node is null.")

    #delete data at chosen position
    def deleteNode(self, position):
        # Checking the Linked List
        if self.head == None:
            return
            # Store head node
        temp = self.head
        # If head needs to be removed
        if position == 0:
            self.head = temp.next
            temp = None
            return
            # Find previous node of the node to be deleted
        for i in range(position - 1):
            temp = temp.next
            if temp is None:
                break
        # If position is more than number of nodes
        if temp is None:
            return2
        if temp.next is None:
            return
            # Node temp.next is the node to be deleted
        # store pointer to the next of node to be deleted
        next = temp.next.next
        # Unlink the node from linked list
        temp.next = None
        temp.next = next
    # Print the linked list
    def printLL(self):
        current = self.head
        while (current):
            print(current.data)
            current = current.next

class Parcel:
    def __init__(parcel1,trackingnumber, couriertype, sendername, senderaddress, sendernumber, receivername, receiveraddress, receivernumber):
        parcel1.trackingnumber= trackingnumber
        parcel1.couriertype = couriertype
        parcel1.sendername = sendername
        parcel1.senderaddress = senderaddress
        parcel1.sendernumber = sendernumber
        parcel1.receivername = receivername
        parcel1.receiveraddress = receiveraddress
        parcel1.receivernumber = receivernumber


    def parcel_info(parcel1):
        print(
            "--------------------------------------------------------------------------------------------------------------------")
        print("1) The tracking number is " + parcel1.trackingnumber)
        print("2) The courier type is   " + parcel1.couriertype)
        print("3) The sender name is " + parcel1.sendername)
        print("4) The sender address is  " + parcel1.senderaddress)
        print("5) The sender number is  " + parcel1.sendernumber)
        print("6) The receiver name is   " + parcel1.receivername)
        print("7) The receiver address " + parcel1.receiveraddress)
        print("8) The receiver number is " + parcel1.receivernumber)


    def __repr__(parcel1):
        return '''
        This is your Parcel Details :-
        Tracking Number = {}
        Courier Type = {}
        Sender Name = {}
        Sender Address = {}
        Receiver Name = {}
        Receiver Address = {}
        Receiver Number = {} 
        '''.format(parcel1.trackingnumber, parcel1.couriertype,parcel1.sendername, parcel1.senderaddress, parcel1.sendernumber,
                  parcel1.receivername, parcel1.receiveraddress, parcel1.receivernumber)

p1 = Parcel("EA24335981137811","Poslaju", "Hazmi", "Cheras", "0123456789", "Amirul","Sabak", "0129874563")
p1.parcel_info()

p2 = Parcel ("ED56256773888290","J&T", "Hafiz", "Nilai", "0132584569","Muhammad","Sungai Besar", "0123694568")
p2.parcel_info()

p3 = Parcel("EX56244333819112","DHL","Kamarul","Kuala Krai","0124785693","Aizuddin","Ampang","0125648973")
p3.parcel_info()

p4 = Parcel("ED66123245321923","Ninja Van","Nurul", "Segamat", "0182369547","Haziq", "Teluk Intan","0123365889")
p4.parcel_info()

p5 = Parcel("EA24387691772510","GDex","Nazura","Banting", "0135548896", "Zack","Tanah Merah","0136695532")
p5.parcel_info()

p6 = Parcel("EE34728666712819","FedEx","Aiman","Puncak Alam","0182655993","Syakirin","Batu Pahat","0185547536",)
p6.parcel_info()

p7=Parcel("EX11222342678125","Pgeon","Razali","Ayer Keroh","0192366536","Nad","Masjid Tanah","0192366399")
p7.parcel_info()

p8=Parcel("EF1324567890123","Skynet","Fitri","Bangi","0162245896","Khad","Shah Alam","0145255485")
p8.parcel_info()

p9=Parcel("EA12456780129098","Citylink","Nabiha","Seremban","0132588523","Balqis","Lumut","0132233556")
p9.parcel_info()

p10=Parcel("ER12356789013456","Poslaju","Alwani","Kajang","0136693653","Tengku", "Tanah Merah","0132904856")
p10.parcel_info()

parcel_LL = LinkedList()
parcel_LL.insert(p1)
parcel_LL.insert(p2)
parcel_LL.insert(p3)
parcel_LL.insert(p4)
parcel_LL.insert(p5)
parcel_LL.insert(p6)
parcel_LL.insert(p7)
parcel_LL.insert(p8)
parcel_LL.insert(p9)
parcel_LL.insert(p10)
parcel_LL.printLL()

while True:

    print("\n[1]Add Node"
          "\n[2]Delete Node"
          "\n[3]Exit Program")
    i = int(input("\nPlease Choose: "))
    if i == 1:
        a = str(input("Tracking No : "))
        b = str(input("Courier Type : "))
        c = str(input("Sender Name : "))
        d = str(input("Sender Address : "))
        e = int(input("Sender Number : "))
        f = str(input("Receiver Name : "))
        g = str(input("Receiver Address: "))
        h = int(input("Receiver Number : "))

        z = Parcel(a, b, c, d, e, f, g, h)



        print("\n[1]Begining"
              "\n[2]Certain Position"
              "\n[3]End")
        a = int(input("\nAdd new Data at: "))
        if a == 1:
            parcel_LL.push(z)
            parcel_LL.printLL()
            continue
        elif a == 2:
            j = int(input("Which Position? : "))
            parcel_LL.push_at(z, j)
            parcel_LL.printLL()
            continue
        elif a == 3:
            parcel_LL.insert(z)
            parcel_LL.printLL()
            continue
    elif i == 2:
        num = int(input("\nRemove node at position: "))
        parcel_LL.deleteNode(num - 1)
        parcel_LL.printLL()
        continue

    elif i == 3:
        break
Updated 1-Dec-21 20:16pm

1 solution

Quote:
and im stuck

Stuck at what? Writing more code? Getting what you have to work? Physically glued to your desk?
Quote:
It should be at least 3 different variables from the original data.

What is that supposed to mean? We have no idea.

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.

"It doesn't work" is probably the most useless problem report we get - and we get it a lot. It tells us nothing about what is happening, or when it happens.
So tell us what it is doing that you didn't expect, or not doing that you did.
Tell us what you did to get it to happen.
Tell us any error messages.

But ... assuming that that code doesn't do exactly what you want it to, then ... getting your code to run at all does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
If you have no idea how to use a debugger, then start here: pdb — The Python Debugger — Python 3.10.0 documentation[^]

This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900