0

Code Problem: Take userinput from a file and execute the class and its function line by line. The userinput will be like a list with 10 or more values. I have reduced the number of items for discussion purpose. I can make the code work and get the output but wondering if there is any cleaner method which can be scaled easily.

Note: Python2.7 cannot use 3.7 yet

##Below is an example code and its output

\\

class student():
    def __init__(self,name,age,club):
        self.name=name
        self.age=age
        self.club=club
    def get_club(self):
        if int(self.age)<10 and self.club=="lego":
            print("Student :"+self.name+' is in: '+self.club+ ' club')
        elif int(self.age)>15 and self.club=="football":
            print("Student :"+self.name+' is in: '+self.club+ ' club')
        else:
            print("Student :"+self.name+' is in: '+self.club+ ' club')
ui=[("Jim,8,lego"),("Dwight,16,football"),("Pam,21,arts")]
for i in ui:
    xy=i.split(',')
    c=student(xy[0],xy[1],xy[2])
    c.get_club()

\\

Code Output

Student :Jim is in: lego club Student :Dwight is in: football club Student :Pam is in: arts club

sm028625
  • 3
  • 3
  • What is the purpose of your if-statement? It seems to always return the same output – Julien Roullé Jan 09 '20 at 19:18
  • @JulienRoullé it's just an example code, i have not submitted the real math problem for ease of understanding. – sm028625 Jan 09 '20 at 19:30
  • @C.Nivs i see it works in python 3.7 , does it also work in 2.7 ? Note; I cannot upgrade to 3.7 yet as there are 100s of python file which needs upgrade slowly ! – sm028625 Jan 09 '20 at 19:37
  • Instead of passing a preformatted list, you could pass a multi-line string (triple quote delimiter) and split by line endings first, which would result in a list similar to the one programmed here. That is a good step forward toward being able to read the list from a file. – RufusVS Jan 09 '20 at 21:22

1 Answers1

0

As a simple example in python2, you'll want to use argument unpacking here.

class X:
    def __init__(self, a, b):
        self.a = a
        self.b = b


a = range(2)
x = X(*a)

x.a
0
x.b
1

To show this for your specific example, I'm using StringIO to simulate a file:

from StringIO import StringIO

ui=[("Jim,8,lego"),("Dwight,16,football"),("Pam,21,arts")]

class Student:
    def __init__(self, name, age, club):
        self.name = name
        self.age = int(age)
        self.club = club

    def get_club(self):
        """
        You'll want to use string formatting here, it makes code much
        more readable, and allows the support of different formats
        """
        if self.age < 10 and self.club == "lego":
            print("Student : {} is in {} ".format(self.name, self.club))

        elif self.age > 15 and self.club == "football":
            print("Student : {} is in {} ".format(self.name, self.club))

        else:
            print("Student : {} is in {} ".format(self.name, self.club))


# you'll want to use `with open(file) as fh:` instead of
# this, which is just an example
fh = StringIO('\n'.join(ui))

for line in fh:
    line = line.strip().split(',')
    # note the * here before the iterable
    c = Student(*line)
    c.get_club()

Student : Jim is in lego
Student : Dwight is in football
Student : Pam is in arts

str.format is portable from python2 to python3, and good on you for using the parentheses on print, that will make your eventual migration journey much easier

C.Nivs
  • 12,353
  • 2
  • 19
  • 44