473,385 Members | 1,824 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help with Classes

Hi I am trying to introduce a class into the code below. The code opens a file with comma separated values (e.g: John, 100). The program gives you a menu 1 to read the file, 2 adds a new student to the file. And 3 calculates the mean and range for all the grades. I am stuck at number 3 which calculates the mean and range for the numbers from the text file.

Below is the Code:


Expand|Select|Wrap|Line Numbers
  1. import math
  2.  
  3. def main():
  4.     menu = eval(input("Select 1 to get grades, 2 to add students, or 3"))
  5.     if (menu ==1):
  6.         #print("test")
  7.  
  8.         fileName = input("Enter filename: ")
  9.         infile = open(fileName,"r")
  10.         data = infile.read()
  11.         print(data)
  12.  
  13.     if (menu ==2):
  14.         #print("test3")
  15.  
  16.  
  17.         filename1 = input("what is the file name?")
  18.  
  19.         file1 = open(filename1, "a")
  20.         file1.close()
  21.  
  22.         studentName =input("what is the student name?")
  23.         studentGrade =input("What is the student grade?")
  24.         file1 = open(filename1, "a")
  25.         file1.write(studentName + "," +studentGrade + "\n")
  26.         file1.close()
  27.  
  28.  
  29.  
  30.     if (menu ==3):
  31.         #print("test3")
  32.  
  33.         filename2 = input("what is the file name?")
  34.         infile = open(filename2,"r")
  35.         sum = 0.0
  36.         count = 0
  37.         for lines in infile:
  38.             name, grade = lines.split(",")
  39.             numbers = grade
  40.             print(numbers)
  41.  
  42.     else:
  43.         print ("Please enter a valid number")
  44.  
  45. main()
Nov 16 '11 #1
2 1044
bvdet
2,851 Expert Mod 2GB
The range would be the highest and lowest grades. The mean would be the grade in the middle. You would need to type cast the grade into an integer and compile the grades into a list to evaluate.
Nov 16 '11 #2
dwblas
626 Expert 512MB
Under option #2 you open and close the file twice. Once is enough.
Expand|Select|Wrap|Line Numbers
  1.   if (menu ==2):
  2.          #print("test3")
  3.  
  4.  
  5.          filename1 = input("what is the file name?")
  6.  
  7.          file1 = open(filename1, "a")
  8. ##         file1.close()      ## close after writing
  9.  
  10.          studentName =input("what is the student name?")
  11.          studentGrade =input("What is the student grade?")
  12. ##         file1 = open(filename1, "a")     ## already open
  13.          file1.write(studentName + "," +studentGrade + "\n")
  14.          file1.close() 
Nov 18 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: rh0dium | last post by:
Hi all, I believe I am having a fundamental problem with my class and I can't seem to figure out what I am doing wrong. Basically I want a class which can do several specific ldap queries. So...
1
by: Robert | last post by:
I have two tables (classes and students). Right now, I'm generating a report in asp that shows a list of classes, the enrollment for each class, and how many seats are available (see query below)....
5
by: Wysiwyg | last post by:
I'm new to c# programming and can't figure out how to avoid duplicating common code in multiple classes when I'm restricted to using different system base classes.. I'm using c# in asp.net to write...
5
by: Anthony Evans | last post by:
Greetings I'm using VC++.NET to create a class library. The class library contains managed classes that wrap legacy unmanaged classes by the same name. I use regasm to register the DLL for...
9
by: Jordan Tiona | last post by:
I can't get this code to work right. It seems to be skipping some of the cin functions. Can someone help me with this? ClassTrack.cpp: #include <iostream> #include "ClassTrack.h" using...
5
by: QbProg | last post by:
Hello, I did some experiments with VC++ 2005, and some ILDasm. Please tell me if I have understood the concepts of C++ programming under .NET, since I'm a bit confused! -- There are 4 types of...
9
by: Chrissy | last post by:
I took a C# class as an elective and received an incomplete in it and am desparate for help. I have two assignments left (arrays and inheritance) and would gladly pay anyone that can assist me with...
173
by: Zytan | last post by:
I've read the docs on this, but one thing was left unclear. It seems as though a Module does not have to be fully qualified. Is this the case? I have source that apparently shows this. Are...
2
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
0
by: atencorps | last post by:
Hello I have the following code but need some help on it. The idea of the code is the main sections ie Service Management are viewable when the page is loaded and by clicking on the main...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.