Dec-15-2017, 04:12 AM
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.
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.
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.