Home > Software engineering >  Working with classes, trying to print multiple things from single input
Working with classes, trying to print multiple things from single input

Time:08-07

I'm following some tutorials about setting up a class and trying to expand the functionality. I've set up a class for 'students' with name, major, gpa, probation status. I've entered 2 students, Student1 and Student2. This is all working great.

I'm trying make it so the user can enter the student's number, for instance '1', and get all four pieces of info. I'm sure I'm going about this all wrong. For now I've coded it to get me the correct string of characters that I would enter after print to get the result I want, but its just printing those characters, not actually getting the values from the class.

class Student:

    def __init__(self, name, major, gpa, is_on_probation):
        self.name = name
        self.major = major
        self.gpa = gpa
        self.is_on_probation = is_on_probation
snum = int(input("Enter Student Number: "))

studentnum = "Student"   str(snum)

info = studentnum   ".name,", studentnum   ".major, ", studentnum   ".gpa, ", studentnum   ".is_on_probation"

from student import Student

Student1 = Student("Bob Lastname", "Finance", 4.0, False)
Student2 = Student("Firstname Lastname", "CS", 4.0, True )


print(studentnum   '.name,', studentnum   ".major,", studentnum   ".gpa,", studentnum   ".is_on_probation")

print(info)

gives me:

Enter Student Number: 1

Student1.name, Student1.major, Student1.gpa, Student1.is_on_probation

('Student1.name,', 'Student1.major, ', 'Student1.gpa, ', 'Student1.is_on_probation')

my goal here is just to enter '1' and have it spit out the same info as if I'd entered

print(Student1.name, Student1.major, Student1.gpa, Student1.is_on_probation)

which prints

Bob Lastname Finance 4.0 False

It seems that I need to find a way to get the result of print(studentnum '.name,', studentnum ".major,", studentnum ".gpa,", studentnum ".is_on_probation") to actually run, then print the result rather than the string, but I can't figure out how to make that happen.

I've also tried a few ways of defining info, but with no good results.

CodePudding user response:

You need to enter the student instances into a collection.

I'm going to use a dict in this example, but you may have other ideas later.

from student import Student

students = { 1:Student("Bob Lastname", "Finance", 4.0, False),
             2: Student("Firstname Lastname", "CS", 4.0, True ) }

snum = int(input("Enter Student Number: "))

selected_student = students[snum]

print(selected_student.name, selected_student.major, selected_student.gpa, selected_student.is_on_probation)

Output:

Bob Lastname Finance 4.0 False

CodePudding user response:

You can do it like that:

snum = int(input("Enter Student Number: "))

studentnum = "Student"   str(snum)

from student import Student

Student1 = Student("Bob Lastname", "Finance", 4.0, False)
Student2 = Student("Firstname Lastname", "CS", 4.0, True )

#Here you get the info
info1 = studentnum   ".name"
info2 = studentnum   ".major"
info3 = studentnum   ".gpa"
info4 = studentnum   ".is_on_probation"

#Here you store them in variables to be printed
exec('name=' info1) 
exec('major=' info2)
exec('gpa=' info3)
exec('is_on_probation=' info4)

print(studentnum   '.name,', studentnum   ".major,", studentnum   ".gpa,", studentnum   ".is_on_probation")

print(name, ' ',major, ' ',gpa, ' ',is_on_probation)

Output:

Student2.name, Student2.major, Student2.gpa, Student2.is_on_probation
Firstname Lastname   CS   4.0   True
  • Related