0

**Goal: Print out average heights and amount of people are in the file(By counting the names) and ask for new input from user, then calculate the new average height **

Problem: There's no output it just outputs "Process finished with exit code 0" the output should be "Enter Your name : "

Here's the code:

class Calutis:
    def init(self):
        self.names=[]
        self.heights=[]
        self.totalheight=0
        self.totalcount=0

    def reset(self):
        self.names = []
        self.heights = []
        self.totalheight = 0
        self.totalcount = 0

    def calAvgHeight(self):
        f=open("listOfStudentHeight.txt","r")
        for line in f:
            info=line.split("\t")
            self.names.append(info[0])
            self.heights.append(float(info[1]))
            self.totalheight+=float(info[1])
            self.totalcount=len(self.names)
            print("the average score of " + self.totalcount + "students is: " + (self.totalheight)/self.totalcount)

    def adduser(self):
        self.names=input("Enter Your Name : ")
        while(True):
            try:
                self.heights=round(float(input("Enter Your Height in Metres")),2)
                break
            except ValueError:
                print("Enter a valid input for height: ")
            try:
                self.names=(str(input("Enter Your Name : ")))
                break
            except ValueError:
                print("Enter a valid input for name : ")

        f=open("listOfStudentHeight.txt",'a')
        f.write(self.names + "\t" + self.heights + "\n")
        f.close()

        calBox = Calutis()
        calBox.adduser()
        calBox.calAvgHeight()
This is the weights and names file content:
CHITRA DEVI D/O SILVARAJAH  1.65
MARSHEAL HOUDEL S/O MATHEWS JA  1.72
MUHAMMAD AZLIE B ZULKIFLIE  1.68
HO QIN YUAN Melvin  1.69
TENG YONG PENG DESMOND  1.76
CHEONG LEE YEE  1.59
MUHAMMAD ZULFIKAR B ZAINAL  1.9
ASYRAFIZWANI BTE ABDUL LATIFF   1.58
HIE BAO XIN 1.63
MAK YU JIE  1.67

Please consider lending me a helping hand, Any help will be greatly appreciated !

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Kai
  • 1
  • 3
  • 2
    I don't see any JavaScript, nor PHP, nor HTML here. Are you sure you've used the tags correctly? – VLAZ Jul 31 '20 at 04:04
  • 1
    That's only the class definition. You need to instantiate the class via something like ```weightCalc = Cal()``` and then run ```weightCalc.calvalue()``` – ewokx Jul 31 '20 at 04:10
  • incidentally, try to also close your file with ```f.close()``` – ewokx Jul 31 '20 at 04:14
  • what does the f.close() do in this code ? – Kai Jul 31 '20 at 07:15
  • ive just tried the codes it still has no output other than "Process finished with exit code 0" – Kai Jul 31 '20 at 07:17
  • 1) You have no main method here. 2) Are you sure you're running the correct file in PyCharm? – OneCricketeer Aug 02 '20 at 03:47

2 Answers2

0

Here are a couple things you need to do

  1. Instantiate the class: calculator = Cal()
  2. Call the adduser function: calculator.addusers()
  3. Call the caluser function: calculator.caluser()

The class definition is not running code. Think of it like a resource that has functionality you can access and call.

Also, is all this in one file? If this class definition is in a separate file, make sure you are actually running the file that contains the 3 lines I mentioned.

Closing the file should not affect the functionality of your code, but it is good practice. After you are done writing to a file, you should call file.close(). As to the specifics of why, it's a more advanced operating systems topic related to file descriptors.

fooiey
  • 1,040
  • 10
  • 23
  • I've done all the advice you guys given me but the output is still "Process finished with exit code 0" – Kai Aug 01 '20 at 03:34
  • Can you add a screenshot showing how you are running the code? Or at least describe what IDE you are using, what you are typing into the command line to run the code, etc.? – fooiey Aug 01 '20 at 04:23
  • im using pycharm and im running it by clicking the green arrow button, im not sure how to check what IDE im using. sorry im new to python – Kai Aug 01 '20 at 05:26
  • Check out this link: https://repl.it/repls/AgreeableWaterySymbol I can run this code and get some output (although there are some bugs). Can you make a simple program with a single main.py file that prints "hello world"? If not, there is something wrong with your setup. – fooiey Aug 01 '20 at 15:18
  • i just tried the code it works so there must be something missing or wrong with my current code – Kai Aug 02 '20 at 02:37
  • It has output but it doesnt check if its a string for name and if its float for height – Kai Aug 02 '20 at 05:42
0

Issue

Your code doesn't appear to have any outputs because the code isn't actually doing anything! :P Specifically, even though you wrote a bunch of lines of code, none of them are actually getting executed.

Explanation

You defined a class called Cal, but you aren't actually using it. A class is like a pre-filled box in that it is a container that holds many useful things, namely functions and variables. The items in the box don't do anything while sitting there - you must actively open the box and grab those items in order to use them. Regarding what this means for writing Python code, you must actually access that class and use its functions/variables in order to do anything with it.

There's another layer here. Writing up a class (like your Cal class) only defines what the pre-filled box looks like. It doesn't actually create the pre-filled box. So maybe a more accurate description of what a class is would be a "blueprint" for a pre-filled box.

Solution

You have to do three things to get any of the code in your class Cal to run:

  1. Define a blueprint for your box (you've already done this by creating class Cal)

This is known as "creating a class definition" or "defining a class"

  1. Create an instance of the box from the "blueprint"

This is known as "creating an instance of a class" or "instantiating a class". The thing that you create is known as an "object" (I know, not as fun as a box haha).

  1. Reach into the box, grab something, and use it

You must access the functions or member variables within a class in order to use them.

Here's how you do these 3 things...

# Step 1: Create the blueprint
# Put your class definition for "Cal" here
#

calBox = Cal()  # Step 2: Create the box (note: parentheses are important here!)
calBox.adduser()  # Step 3: Use something from the box (eg: call one of your functions!)

And as it turns out, your "blueprint" can be used to create many boxes! So you can have 3, 4, 5... however many you want :)

calBox1 = Cal()  # Create a box (i.e. an instance of the Cal class)
calBox2 = Cal()  # Create another box (i.e. another instance)
calBox3 = Cal()  # Create anotherrrr box :P

That's it! Add those two lines of code below and outside of your Cal class definition. Then you should see some output! :)

  • I've just tried this but the output is still just "Process finished with exit code 0" – Kai Aug 01 '20 at 03:34
  • @Kai I tried to run the code myself with the two additions I mentioned in my answer. I am able to get output (although you have some errors haha). Maybe you're not running the script correctly. Reference https://stackoverflow.com/questions/21492214/how-to-run-python-script-on-terminal – Grant Schulte Aug 01 '20 at 03:52
  • i am currently required to use pycharm for this assignment, idk if that makes a difference but would u care to tell me about the errors ? – Kai Aug 01 '20 at 05:23
  • i ran the code by clicking the green arrow button on pycharm but my teacher also used to same app and code but his had output asking the user to type your name and height. Checking if input was float or string – Kai Aug 01 '20 at 05:30
  • Not sure how PyCharm works, but try putting the extra 2 lines from my answer into this if-check: `if __name__ == "__main__":` (new line) `calBox = Cal()` (new line) `calBox.adduser()` – Grant Schulte Aug 01 '20 at 17:48
  • If that doesn't work, maybe try setting up your PyCharm from scratch and paste in your current code. See https://www.jetbrains.com/help/pycharm/creating-and-running-your-first-python-project.html – Grant Schulte Aug 01 '20 at 17:51
  • Also, are the two extra lines from my answer inside the class? If so, take them out. They should not be indented at all. That’s the last thing I can think of sorry! – Grant Schulte Aug 02 '20 at 04:09