Aug-28-2018, 01:17 PM
To be honest, whenever I see something like your code, I think "how can I loop this?" That is, whenever a programmer is doing something over and over again, they should think about putting it in a loop. At least, if it's all in the same place like your code is. If you are doing the same thing in different places, you should think about making a function, and calling that function from those different places.
So, in your code you are asking questions and assigning them to parameters in an instance creation. Some of them you convert to int. How could we make that into a loop?
Here, this technique is kind of borderline useful. My code takes 13 lines where yours took 12. However, in more complicated situations it can clean up your code an incredible amount. This code is easier to update. Say you decided later on all responses should be stored in upper case. In my code you would only have to change one line. In your code, you would have to change 10.
So, in your code you are asking questions and assigning them to parameters in an instance creation. Some of them you convert to int. How could we make that into a loop?
class Studentform: questions = {'fname': "First Name: \n", 'lname': "Last Name: \n", 'email': "Email: \n", 'ccode': "Country code: \n", 'mobile': "Phone number: \n", 'github': "Github profile: \n", 'country': "Country: \n", 'identity': "Identity (Woman/Non Binary/Man): \n", 'want': "I want to (Find my next job/Become a freelancer/Start my own tech startup): \n", 'codelevel': "My coding level is (Beginner/Intermediate/Advanced): \n", 'subs': "I want to subscribe to and join the following groups (Student/Working/Job seeking/Entrepreneur/Mom/Single Mom/Non-Binary/Trans/Retired/Career changer): \n", 'like': "I would also like to (Volunteer/Mentor/Hire): \n"} to_int = ['ccode', 'mobile'] def __init__(self, fname, lname, email, ccode, mobile, github, country, identity, want, codelevel, subs, like): self.fname = fname self.lname = lname self.email = email self.ccode = ccode self.mobile = mobile self.github = github self.country = country self.identity = identity self.want = want self.codelevel = codelevel self.subs = subs self.like = like @classmethod def raw_input(cls): kwargs = {} for parameter, question in self.questions.items(): kwargs[parameter] = input(question) if parameter in self.to_int: kwargs[parameter] = int(kwargs[parameter]) return cls(**kwargs) user = Studentform.raw_input()This makes use of the ** operator. In parameter lists, it turns a dictionary into key word parameters, using the key/value pairs of the dictionary as parameter/value pairs.
Here, this technique is kind of borderline useful. My code takes 13 lines where yours took 12. However, in more complicated situations it can clean up your code an incredible amount. This code is easier to update. Say you decided later on all responses should be stored in upper case. In my code you would only have to change one line. In your code, you would have to change 10.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures