Shnigams
Badges: 2
Rep:
?
#1
Report Thread starter 3 weeks ago
#1
Hi all, I need help with answering the following question for my programming homework. I have watched many videos and read many threads but I still can't answer it.
Question:
Create a class Student with public member variables: Student name, student number, contact number, ID number. The following specifications are required:
 Add init() method of the class that initializes string member variables to empty strings and numeric values to 0.
 Add the method populate() to the class. The method is used to assign values to the member variables of the class.
 Add the method display() to the class. The method is used to display the member variables of the class.
 Create an instance StudentObj of the class Student in a main program.
 The main program should prompt the user to enter values for five students. The attributes should be assigned to the instance of Student using its populate() method, and must be displayed using the display() method of the instance of Student.

My attempt:
I can create the class and I think I am adding the variables as public members because I didn't add any underscore...see my attempt below:
Class Student:
def__init__(self, student name, student number, student conctact, ID number)

But after this part I am lost...I am not sure I understand the rest of the question.

Will appreciate any guidance, tips or advice anyone can offer.
Thanks.
0
reply
Mr Wednesday
Badges: 13
Rep:
?
#2
Report 3 weeks ago
#2
(Original post by Shnigams)
Hi all, I need help with answering the following question for my programming homework.

Create a class Student with public member variables: Student name, student number, contact number, ID number. The following specifications are required:
Not a solution to your problem, but a general comment on how to ask for help with code.

Its extremely tough to debug code remotly when you cant see what someone has attempted. For short sub 10 line snippets of code, just post them along with the question so that its immediately clear what you have tried. For bigger blocks of code, why not try Github or similar to archive your work and let others access it easily. Thats also a useful learning excercise in its own right.
0
reply
winterscoming
Badges: 19
Rep:
?
#3
Report 3 weeks ago
#3
(Original post by Shnigams)
My attempt:
I can create the class and I think I am adding the variables as public members because I didn't add any underscore...see my attempt below:
Class Student:
def__init__(self, student name, student number, student conctact, ID number)
If this is your actual code then you need to go all the way back to basic principles about Python syntax because what you've pasted here isn't valid python. There are several problems:
  • Spaces are not allowed in variable names
  • the class keyword' must be all-lowercase - you can't start it with an uppercase letter
  • there must be a space in-between the def keyword and __init__
  • You are missing a colon : after the end of the closing-brace of __init__

Even if your real code doesn't have those errors and you've got it working, I'd suggest taking a step back and spending time (at least a couple of hours) studying this lesson on Object-oriented programming in Python to try to understand the concepts and see the example code used in the lesson - https://www.py4e.com/lessons/Objects - you really need to be able to relate the instructions you've been given to these concepts and understand what it all means before you sit down and try to actually write the code.

The Py4E lesson explains all the concepts and terminology you need to be able to complete the assignment you've been given, as well as the syntax. It will explain what a 'method' is (and how a method is different to a plain function), how to create objects (instances) of your student class, as well as a lot of other things which should make everything much clearer.
.
There's also lots of small, simple code examples in the videos and on the notes for the lessons here: https://www.py4e.com/html3/14-objects

You should paste the code examples given into a new Python program and run those examples on their own to see how they work and check that you understand the syntax - you use those code examples as a starting-point as well by modifying them to fit your needs.
(Also remember that indentation is super-important in Python..)


Also remember that you should try to start off with doing the simplest thing possible first -- so before you try to get a program working with 5 student objects (instances), try to get something which works with just 1 student object (instance) instead -- if you can do it with 1, then adding another 4 students is just a matter of repetition.


Also as the other poster mentioned, TSR isn't very good at posting code snippets - If you want to post snippets of code, it's useful to create a sample using a site like Repl.It instead - https://repl.it/languages/python3
Last edited by winterscoming; 3 weeks ago
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

University open days

  • Bournemouth University
    Midwifery Open Day at Portsmouth Campus Undergraduate
    Wed, 16 Oct '19
  • Teesside University
    All faculties open Undergraduate
    Wed, 16 Oct '19
  • University of the Arts London
    London College of Fashion – Cordwainers Footwear and Bags & Accessories Undergraduate
    Wed, 16 Oct '19

How has the start of this academic year been for you?

Loving it - gonna be a great year (134)
17.72%
It's just nice to be back! (204)
26.98%
Not great so far... (271)
35.85%
I want to drop out! (147)
19.44%

Watched Threads

View All