Python Forum
Troubleshooting simple script and printing class objects
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Troubleshooting simple script and printing class objects
#8
To answer your question #3 in reguards to your latest post (#7) I would made a loop
that inputs each value from the user into some temperary variable and then use that
to populate the citizen256032 instance. Here is some code I wrote that works
but leaves alot of room for improvement.

def main():
    # citizen256032 = PassPort('Jon', 'Ross', 'Dobbs', 'male', 1947, 44)
    # first, middle, last, gender, yob, iq
    # initalize a list with the default values, inputs will replace these
    person = ['Jon','Ross','Dobbs','male',1947,44]

    EOF = False;  #End of File, I use this as a flag to stop the while loop

    while not EOF:
        person[0] = input('First Name:')
        while not person[0].isalpha(): # Keep asking if input contains anything other than letters
            print('Please enter a valid first name')
            person[0] = input('First Name:')
        person[1] = input('Middle Name:')
        while not person[1].isalpha():
            print('Please enter a valid middle name')
            person[1] = input('Middle Name:')
        person[2] = input('Last Name:')
        while not person[2].isalpha():
            print('Please enter a valid last name')
            person[2] = input('Last Name:')
        print('Please select your gender\n')
        person[3] = int(input('\t0) Female\n\t1) Male\n'))
        while (person[3] not in range(0,2)):
            print('Please enter a value of either 0 or 1')
            person[3] = int(input('\t0) Female\n\t1) Male\n'))
        person[4] = int(input('What year were you born? (nnnn): '))
        while (person[4] not in range(1800,2999)):
            print('Please enter a four digit number')
            person[4] = int(input('What year were you born? (nnnn): '))
        person[5] = int(input('What is your IQ?: '))
        while (person[5] not in range(0,160)):
            print('Please enter a number between 0 and 160 inclusive')
            person[5] = int(input('What is your IQ? (0-160): '))
        EOF = True;

    if (person[3] == 0):
        gender = 'male'
    else:
        gender = 'female'

    citizen256032 = PassPort(person[0],person[1], person[2], gender, person[4], person[5])
    print(citizen256032)
Here is my explanation of what I have done

EOF is just what I use to mark the end of the while loop, so after all input is gathered I mark EOF as false thus causing the while loop to break.

person is a list which I populated with default values, that once the user enters their
values, gets replaced with the users repsonce. Its sole purpose is to store the data while it is being collected

I get input for each value and then each time I sanitize the input from the user to verify that it meets the basic expectations, for example a name should not contain numbers and a year of birth should not be a string. In this case isalpha() will return true if the value given contains only letters, but I want to know if the user did NOT enter a purely alphabetic string in such case I want to continue looping for input untill the condition fails, which in this case means they did enter what was expected

to force person[3], person[4] and person[5] values to be integers, I enclose the input inside int(). I then test these using a contriversal method wherre range returns a list of values in that range which are then checked against the input. This can be considered slow and it is probably faster to use something like (0 <= person[5] <= 160) to check if the value is within range.

Because I gather 1 for male and 0 for female, and your object requires a string, I use the finaly if else block to create a gender variable which is the expected string

I hope this makes some sence

Like I said there are probably better ways to do this, for example I could have written a function that does the job of asking for input untill a user enters all letters, instead of rewriting that code over and over again. Also this is aproaching database teritory where books on the subject of coding such a database in various languages have been written. In this case the user input is thrown into memory which is then not accessable after the script ends.

Hope this helped somewhat.
Reply


Messages In This Thread
RE: Troubleshooting simple script and printing class objects - by knackwurstbagel - Dec-15-2017, 04:12 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Troubleshooting Jupyter Notebook installation with Python using pip and a venv Drone4four 0 83 Yesterday, 01:00 AM
Last Post: Drone4four
  Printing out incidence values for Class Object SquderDragon 3 436 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  OBS Script Troubleshooting Jotatochips 0 360 Feb-10-2024, 06:18 PM
Last Post: Jotatochips
  How can I access objects or widgets from one class in another class? Konstantin23 3 1,150 Aug-05-2023, 08:13 PM
Last Post: Konstantin23
  Troubleshooting with PySpy Positron79 0 870 Jul-24-2022, 03:22 PM
Last Post: Positron79
  Simple Python script, path not defined dubinaone 3 2,795 Nov-06-2021, 07:36 PM
Last Post: snippsat
  Troubleshooting site packages ('h5py' to be specific) aukhare 2 2,072 Nov-02-2020, 03:04 PM
Last Post: snippsat
  Class objects Python_User 12 4,624 Aug-27-2020, 08:02 PM
Last Post: Python_User
  How to create and define in one line a 2D list of class objects in Python T2ioTD 1 2,128 Aug-14-2020, 12:37 PM
Last Post: Yoriz
  OOP hellp (simple class person) onit 4 2,620 Jul-14-2020, 06:54 PM
Last Post: onit

Forum Jump:

User Panel Messages

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