|
| 1 | +# Lab Instructions: Import and Scope |
| 2 | + |
| 3 | +So far, you've learned the different ways in which you can use import statements to import other Python files, modules and packages. |
| 4 | +You have also seen the different ways in which you can import specific functions using different formats of import. |
| 5 | +In this assignment you'll learn and practice how to use import to bring external code within the direct scope of the project. |
| 6 | + |
| 7 | + <br> |
| 8 | + |
| 9 | +> ### **Tips: Before you Begin** |
| 10 | +> #### **To view your code and instructions side-by-side**, select the following in your VSCode toolbar: |
| 11 | +> - View -> Editor Layout -> Two Columns |
| 12 | +> - To view this file in Preview mode, right click on this README.md file and `Open Preview` |
| 13 | +> - Select your code file in the code tree, which will open it up in a new VSCode tab. |
| 14 | +> - Drag your assessment code files over to the second column. |
| 15 | +> - Great work! You can now see instructions and code at the same time. |
| 16 | +> - Questions about using VSCode? Please see our support resources [here](https://www.coursera.org/learn/programming-in-python/supplement/2IEyt/visual-studio-code-on-coursera). |
| 17 | +> #### **To run your Python code** |
| 18 | +> - Select your Python file in the Visual Studio Code file tree |
| 19 | +> - You can right click the file and select "Run Python File in Terminal" |
| 20 | +> or run the file using the smaller |
| 21 | + play button in the upper right-hand corner |
| 22 | +> of VSCode. |
| 23 | + (Select "Run Python File in Terminal" in the provided button dropdown) |
| 24 | +> - Alternatively, you can follow lab instructions which use python3 commands to run your code in terminal. |
| 25 | +> |
| 26 | +
|
| 27 | +<br> |
| 28 | + |
| 29 | +## Exercise Objectives: |
| 30 | +- Use the import statement to import a built-in package in Python. |
| 31 | +- Use the import statement to call a function present in another Python file. |
| 32 | +<br><br> |
| 33 | + |
| 34 | +## Instructions |
| 35 | + |
| 36 | +1. Open the file jsongenerator.py present inside project folder. |
| 37 | + |
| 38 | +2. Import a built-in package called `json` |
| 39 | + |
| 40 | +3. Import the following from a file called employee.py: |
| 41 | + - A function called `details` |
| 42 | + - Variables called `employee_name`, `age` and `title` |
| 43 | +<br><br> |
| 44 | + |
| 45 | +4. Implement the `create_dict()` function that returns a dictionary given employee information. |
| 46 | +Create and return a dictionary with three key-value pairs where: |
| 47 | + - Keys are string variables: `"first_name"` `“age”` and `“title”` |
| 48 | + and their respective values are `employee_name`, `age` and `title` variables that we have imported from the employee module. |
| 49 | + - Be sure to cast the values to the expected types. |
| 50 | +<br><br> |
| 51 | + |
| 52 | +5. Use a function called `dumps()` from the json module using dot notation and pass the `employee_dict` dictionary that we have created to it. |
| 53 | +Return its value to a variable named `json_object`. |
| 54 | + |
| 55 | + The format of the same should look like: |
| 56 | + ``` |
| 57 | + variable = json.dumps(dict) |
| 58 | + ``` |
| 59 | + |
| 60 | +6. Complete the `write_json_to_file()` function |
| 61 | + - Use a built-in function called `open()` and pass the `output_file` argument and `“w”` to it. |
| 62 | + Return the value of this function to a variable named newfile. |
| 63 | + - Call a function called `write()` over this variable newfile. Pass the `json_object` variable you created in Step 5 inside it. |
| 64 | + - Close this file by calling a built-in function `close()` directly on newfile. You don’t need to pass any arguments here. |
| 65 | +<br><br> |
| 66 | + |
| 67 | + |
| 68 | +7. Save the files |
| 69 | + |
| 70 | +8. Open the terminal to execute the files |
| 71 | + |
| 72 | +9. Run the code using the command (within project directory) |
| 73 | + ``` |
| 74 | + python3 jsongenerator.py |
| 75 | + ``` |
| 76 | +
|
| 77 | +<br> |
| 78 | +
|
| 79 | +
|
| 80 | +## Final Step: Let's submit your code! |
| 81 | +Nice work! To complete this assessment: |
| 82 | +- Save your file through File -> Save |
| 83 | +- Select "Submit Assignment" in your Lab toolbar. |
| 84 | +
|
| 85 | +Your code will be autograded and return feedback shortly on the "Grades" tab. |
| 86 | +You can also see your score in your Programming Assignment "My Submission" tab. |
| 87 | +<br> <br> |
0 commit comments