def print_instructions(): # Prints out the instructions print "Please select an action:" print "1. Add a student" print "2. Print all students" print "3. Print all majors" print "4. Look up a student's major by name" print "5. Quit" def get_option(): option = int(input("Choose an option: ")) if option != 1 and option != 2 and option != 3 and option != 4: print "Error. Please enter again." option = get_option() return option def main(): # Calls print_instructions() # Allows the user to interact with the database (lists) # Uses a while loop to allow the user to continuously use the database (lists) all_students = ['Ellen', 'Sam', 'Victoria', 'Rachel', 'Austin'] all_majors = ['Information Library Science', 'English', 'Computer Science', 'History', 'Chemistry'] print_instructions() option = get_option() while option != 5: if option == 1: student = raw_input("What is the new student's name? ") all_students.append(student) major = raw_input("What is the new student's major? ") if major in all_majors: print major, "is aleady in the list." all_majors.append(major) elif option == 2: for val in all_students: print val elif option == 3: printed_majors = [] for val in all_majors: if val in printed_majors: else: printed_majors.append(val) for val in printed_majors: print val else: lookup_name = raw_input("Please enter the name of the student: ") idx = 0 length = len(all_students) while idx <length: if lookup_name == all_students[idx]: break idx += 1 if idx == length: print "Error. The name of the student does not exist." else: print "The name of the student:", lookup_name print "The major of the student:", all_majors[idx] print_instructions() option = get_option() print "Done" main()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question