2

I am very new to python and am currently taking a class in it at my university. I am stuck on a program that my professor wants us to write. I think that I have most of what he wants working except for correctly passing the variables to the method getClass in the class Traffic. Here is the given assignment:

Step 4:

Steps:

  • Modify the class called Traffic that will have four private attributes: throughput, delay, jitter, loss.
  • Modify the method called getClass within the Traffic class, that will classify traffic as below:
    • Best effort: throughput < 5, delay between 8 and 10
      or
      Throughput between 5 and 10, delay > 8
    • Controlled load: throughput between 5 and 10 , delay <= 8
      or
      Throughput >=10, delay >= 5
    • Guaranteed: throughput >=10, delay < 5

Write a program called testTrafficClass to test the traffic class. The program will have a main() function that will initialize the four attributes of class Traffic, and print the traffic class. It will then prompt the user to change the attributes, and will print traffic class based on the new values.

Here is the code I have so far:

def Main():
    def __init__(self, throughput = 0, delay = 0, jitter = 0, loss = 0):
        self.__throughput = throughput
        self.__delay = delay
        self.__jitter = jitter
        self.__loss = loss

    throughput = eval(input("Enter Throughput: "))
    delay = eval(input("Enter Delay: "))
    jitter = eval(input("Enter Jitter: "))
    loss = eval(input("Enter Loss: "))

    Traffic.getClass(self, throughput, delay)

class Traffic:
        def getClass(self, throughput, delay):
            if (throughput<5) and (delay <= 10) and (8<=delay):
                print("Best Effort")
            if (5<=throughput) and (throughput<=10) and (delay>8):
                print ("Best Effort")
            if (5<=throughput) and (throughput<=10) and (delay<=8):
                print ("Controlled load")
            if (throughput >=10) and (delay >=5):
                print ("Controlled load")
            if (throughput >=10) and (delay <5):
                print ("Guaranteed")

Main()

I'm sure this isn't the best or most elegant code there is since I am very new to Python. If someone could get me on the right track that would be great. I keep getting errors when I run it.

2
  • 2
    You say I keep getting errors when I run it, but exactly what errors are you getting. Commented May 27, 2014 at 23:46
  • Is your __init__ method really in main? It seems like it should be in the Traffic class definition. Commented May 27, 2014 at 23:58

1 Answer 1

4

The problem is that you have not yet actually instantiated an instance of the Traffic class when you attempt call a method from it here:

Traffic.getClass(self, throughput, delay)

I think you should probably read the python class documentation to get a better idea of how classes work, but the quick fix for your solution would be replace that line with the following:

 traffic = Traffic() # Creates an instance of the Traffic class
 tclass  = traffic.getClass(throughput, delay) # Calls the getClass method on that instance

In addition, are you in Python 2.7 or Python 3? Either way, calling eval on input is very bad practice (in the case of 3) or completely unneeded (in the case of 2.7). If the expected input is float you should do this instead:

 throughput = float(raw_input("Enter Throughput: ")) # Python 2.7
 throughput = float(input("Enter Throughput: "))     # Python 3.X

And likewise for the rest of your inputs. This ensures that the only valid input is something that turns into a float, anything else will raise an exception. The way you have it now, the user could enter any arbitrary python code and it would execute, which is a very, very bad thing.

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

2 Comments

In addition to this, your __init__ method should be inside Traffic, not Main
@aruisdante Thanks. This got it working. I know the code is very messy but this is just a very beginning assignment. He just wants us to get the basics down first. Thanks for the help.

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.