Python Forum
[Help] Using "Class" with User Input in an online sign up form
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Help] Using "Class" with User Input in an online sign up form
#8
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?

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
Reply


Messages In This Thread
RE: [Help] Using "Class" with User Input in an online sign up form - by ichabod801 - Aug-28-2018, 01:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to revert back to a previous line from user input Sharkenn64u 2 1,204 Dec-28-2024, 08:02 AM
Last Post: Pedroski55
  User input with while loops chizzy101010 2 6,859 Aug-25-2024, 06:00 PM
Last Post: chizzy101010
  Does @ at sign used for tother than decorators? ggpython000 1 1,182 Oct-12-2023, 09:08 AM
Last Post: buran
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 2,701 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  pyscript index error while calling input from html form pyscript_dude 2 2,093 May-21-2023, 08:17 AM
Last Post: snippsat
  restrict user input to numerical values MCL169 2 1,993 Apr-08-2023, 05:40 PM
Last Post: MCL169
  class Update input (Gpio pin raspberry pi) caslor 2 1,868 Jan-30-2023, 08:05 PM
Last Post: caslor
  user input values into list of lists tauros73 3 2,083 Dec-29-2022, 05:54 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 2,483 Dec-25-2022, 03:00 PM
Last Post: askfriends
Question Take user input and split files using 7z in python askfriends 2 2,463 Dec-11-2022, 07:39 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020