Code Icon

PYTHON ADVANCED

Links


    # TRY EXCEPT (Try-catch)
    try:
        answer = 10/0
        number int(input("Enter a number"))
        print(number)
    except ZeroDivisionError as err:
        print(err)
    except ValueError:
        print("Invalid input")


    # READING FILES
    # r:read; w:write, a:append

    employee_file = open("employees.txt", "r") 
    print(employee_file.readable())
    print(employee_file.readline())
    employee_file.close()


    # WRITING FILES
    employee_file = open("employees.txt", "a") 
    employee_file.write("\nToby - React Developer")
    employee_file.close()



    # USE OF OTHER FILES 
    # IN one file name: import_tools
    import random

    feet_in_mile = 5280
    meters_in_km = 1000
    beatles = ["Lennon", McCartney, Harrison, Star]

    def get_file_ext(filename):
        return filename[filename.index(".") + 1]
    
    def roll_dice(num):
        return random.randint(1, num)

    # IN ANOTHER FILE
    import useful_tools
    print(useful_tools.rolldice(5))


    # MODULES AND PIPS 
    # EXTERNAL LIBRARIES, LIB FOLDER
    with the terminal cmd
    cls
    pip --version #to check
    pip install python-docx
    


    # CLASSES AND OBJECTS
    class Student:
        def __init__(self, name, major, gpa, is_on_probation):
            self.name = name
            self.major = major
            self.gpa = gpa
            self.is_on_probation = is_on_probation

    # ANOTHER FILE, we create a student
    from Student import Student # from Student file import Student class
    student1 = Student("Jim", "Business", 3.1, False)
    print(student1.gpa)



    # BUILDING A MULTIPLE CHOICE QUIZ

    # Class Question
    class Question:
        def __init__(self, prompt, answer):
            self.prompt = prompt
            self.answer = answer

    # Different file
    from Question import Question 
    question_prompts = [
        "What color are Apples?"\n(a) Red \n(b) Purple\n(c) Orange\n\n",
        "What color are Bananas?"\n(a) Brown\n(b) Yellow\n(c) Blue\n\n",
        "What color are Strawberries?"\n(a) Pink \n(b) Black\n(c) Red\n\n"
    ]

    questions = [
        Question(question_prompts[0], "a"),
        Question(question_prompts[0], "b"),
        Question(question_prompts[0], "c")
    ]

    def run_test(questions):
        score = 0
        for question in questions:
            answer = input(question.prompt)
            if answer == question.answer:
                score += 1
        print("You got " + str(score) + "/" str(len(questions)) + "Correct")

    run_test(questions)


    # OBJECT FUNCTIONS

    class Student:
        def __init__(self, name, major, gpa):
            self.name = name
            self.major = major
            self.gpa = gpa

        def on_honor_roll(self):
            if self.gpa >= 3.5:
                return True
            else: 
                return False

    # ANOTHER FILE
    from Student import Student 
    student1 = Student("Jim", "Business", 3.9)
    student2 = Student("LOuise", "Psychology", 2.9)

    print.student1.on_honor_roll())
    print.student2.on_honor_roll())


    # INHERITANCE
    class Chef:
        def make_chiken(self):
            print("The chef makes a Chicken")
        def make_soup(self):
            print("The chef makes a soup")
        def make_special_dish(self):
            print("The chef makes barbeque ribs")

    #CHinese Chef with inheritance, can use the Chef methods
    from Chef imprt Chef

    class ChineseChef(Chef):
        # override method
        def make_special_dish(self):
        print("The chef makes orange chicken")

        # new method
        def make_fried_rice(self):
            print("The chef makes fried rice")

    # Programa
    from CineseChef import CineseChef

    myChineseChef = ChineseChef()
    myChineseChef.make_chicken
    myChineseChef.make_fried_rice



    # PYTHON INTERPRETER
    # terminal: Interact with computer without graphic interface, only with text commands
    cmd 
    python 3 # in windows might not work. Google to add python3 to the path variable

    # You can use the terminal to test stuff