-1

Trying to make a test program that will test three classes(Student and Instructor are subclasses of Person) and I got most of the way there with help from my professor. Downside is, my code is allowing me to write student information just fine, but when I try to run it for instructor information, it defaults back to student info. I've tried adjusting and #ing out where the student prompts would go, but it gives me the error of 'list index out of range'. If anyone could take a look at the code and see where I went wrong, that'd be great.

#import student, instructor, and person


from Student2 import Student
from Instructor2 import Instructor
from person import Person

#create function for asking what type of person we're inputting
def what_type_of_person():
    
    #while we're not done
    not_done = True
    while not_done:
        #ask what type of person, if student or instructor
        #get first letter of response and put it in uppercase
        type_of_person = input('Are you entering (S)tudents or (I)nstructors? ')[0].upper()

        #if the input is either S or I
        if type_of_person in ('S', 'I'):

            #we're not done and move on
            not_done = False

        #Otherwise raise error
        else:
            print('Invalid input. Please try again.')

    #return type_of_person
    return type_of_person


#create function of finding out how many people we're getting
def get_how_many(title):

    #not done is considered true right now, which means we aren't done getting people to get info from
    not_done = True
    
    #so while we're not done, we make a try/except program
    while not_done:

        #trying to
        try:
            #get a quanity of 'how many people are we getting info from?'
            qty = int(input(title))

            #if they input less than 0, we're done and don't need to do anything
            if qty > 0:
                not_done = False

            #otherwise, if they do input <1, display the error
            else:
                print('You must have at least 1 data point to enter. Please try again.')

        #otherwise, print an exception
        except:
            print('Invalid input! Please try again.')

    #return the quantity of people
    return qty

        
#create function for asking questions, passing in what_type
def create_prompts(what_type):

    #ask for the first, middle, last name dob
    prompts = ['First name: ', 'Middle name: ', 'Last name: ', 'Date of birth (mm/dd/yyyy): ']

    #if what_type is I
    if what_type == 'I':

        #add prompts for instructor
        prompts.append('Salary: ')
        prompts.append('Degree (PhD or MS): ')
        prompts.append('Department (CIS, CNG, CSC): ')

    #otherwise add prompts for student
    if what_type == 'S':
        prompts.append('Major: ')
        prompts.append('GPA: ')
        prompts.append('Class year (FR, SO, JR, SR): ')

    #return prompts
    return prompts

#create function for loading list, pass in what_type and how_many
def load_list(what_type, how_many):

    #make the create_prompts function labelled as 'prompts'
    prompts = create_prompts(what_type)

    #empty list
    list_of_people = []

    #for each person they want to get info from
    for i in range(how_many):
        try:
            #make an empty list called answers
            answers = []
            #for every prompt in prompts
            for prompt in prompts:
                #append the answers in prompt to the answers list
                answers.append(input(prompt))

            #if the type of person is an instructor
            if what_type == 'I':
                #create a variable containing all answers 1-7 from instructor
                to_append = Instructor(answers[0], answers[1], answers[2], answers[3], answers[4], answers[5], answers[6])

            #otherwise create a variable containing all answers 1-7 from student
            else:
                to_append = Student(answers[0], answers[1], answers[2], answers[3], answers[4], answers[5], answers[6])

            #append this variable to the list of people list
            list_of_people.append(to_append)

        #if none of that works, create an exception
        except Exception as e:
            print(e)
            print("You must re-enter this individual's information. Refresh and try again")

    #return the list of people
    return list_of_people

#create function for printing people, pass in what_type and load_list
def print_people(what_type, load_list):

    #if the type of person is an instructor
    if what_type == 'I':
        #print following instructor statement
        print('Instructor list is: ')

    #otherwise print student statement
    else:
        print('Student list is: ')

    #for each person in the list
    for person in load_list:
        #print their information
        print(f'{person}\n')



def  main():

    #pass what type of person and label it as person type
    #(because python doesn't like multiple variables of the same name
    person_type = what_type_of_person()
    
    #creating the question to ask how many people
    how_many = get_how_many('How many people do you want to create? ')

    #get the load list, passing on what_type_of_person and how_many
    #and label it as list_of_people
    list_of_people = load_list(what_type_of_person, how_many)

    #pass on that list and the person_type to the print function
    print_people(person_type, list_of_people)
    


if __name__ == '__main__':
    main()

I've done a bit of digging and right around the 'create prompts' function is where it seems to go wrong. I tried moving a few things and removing others, but nothing seemed to work. If you need the 3 class python programs I have, please let me know. Anything helps!

SlipScout
  • 13
  • 4
  • 1
    Please include the full error traceback. – ewokx Mar 31 '22 at 02:29
  • Preliminary scan of your code. ```if type_of_person == 'S' or 'I':``` doesn't really do what you think it does. Please take a look at https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-for-equality-against-a-single-value – ewokx Mar 31 '22 at 02:32
  • @ewong Yup, fixed that. I'll edit it in the post but it should be if type_of_person in ['S', 'I']: instead – SlipScout Mar 31 '22 at 02:33

1 Answers1

1

I see a few issues, but the biggest one is right here:

if type_of_person == 'S' or 'I':

This line of code, in English, says "IF the variable named type_of_person, which is on the LEFT side of the ==, has the same value as whatever is on the RIGHT side of ==, then execute the indented code below.

Now, what is on the right side? You have:

'S' or 'I'

This, in English means "If 'S' evaluates as True, or 'I' evaluates as True, then the value of the expression is True.

Since any string with a non-zero length will always evaluate as True when evaluated as a Boolean, the right side of your == is always a value of True.. it is NOT the letters in question so it'll never match type_of_person.

What you want is:

if type_of_person in ('I', 'S'):

or

if type_of_person == 'I' or type_of_person == 'S':

These comparisons compare a string to a string, which is what you want to do.

little_birdie
  • 5,600
  • 3
  • 23
  • 28
  • Hi! Yes, thank you so much. Just fixed it in the post, I was messing around with it and forgot to change it back. What other issues did you see? I've been staring at this code for hours – SlipScout Mar 31 '22 at 02:38