Skip to content

WDI-SEA/M4L3-python-containers-wbp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python Containers - Work Based Problem

scenario

Y Language fever has gripped the city! Hot on the heels of Triangle announcing their decision to switch to Y Language, ROS - Retro Operating Systems has announced the same thing!

(In this exercise, X = Javascript and Y = Python. So you'll be rewriting a program originally written in Javascript into Python!)

requirements

  • Write Python version of the Contacts program in main.py
  • Write Python version of the Contacts program in contacts.py
  • Use basic Python syntax including:
    • List [] syntax
    • Dictionary {} syntax
    • Iterating through lists
    • Appending and Removing items from lists
    • while loops
    • match statement (same as switch in Javascript)
    • String interpolation using f-strings
    • And everything else from the previous problem!

tickets

trello board

coding practice

Step 1:

  • Fork and clone thishttps://git.generalassemb.ly/SEI-Standard-Curriculum/M4L3-python-containers-wbp repository
  • Navigate (cd) into the repository folder in your console
  • Open up the repository folder in your code editor
    • If you're using Visual Studio Code, the command is code .
  • In your terminal, navigate into the js directory -- this directory contains the working Javascript program
    • Run the command npm install -- this will install the modules necessary for the automated tests to run
  • Run the automated tests with npm test. You should see:
  % npm test

  > [email protected] test
  > jest

   PASS  test/contacts.test.js
   PASS  test/main.test.js

  Test Suites: 2 passed, 2 total
  Tests:       10 passed, 10 total
  Snapshots:   0 total
  Time:        0.18 s, estimated 1 s
  Ran all test suites.
  • Open up js/src/main.js and js/src/contacts.js. These files contain the working Contacts program in Javascript
    • Read it over and understand it -- it should be simple for you at this stage of the game!
  • Run the program with node src/main.js. Play with it and understand its operation
  • You do not need to touch anything in the js/test directory; those are the automated tests

Step 2:

  • Now, open up python/src/main.py and python/src/contacts.py
  • You'll be writing the Python version of the Contacts program in these two files
  • To run the program, navigate to the python directory in your terminal and run python src/main.py
    • Build your program up bit by bit and make sure it runs correctly! Run it often!
  • You do not need to touch anything in the python/test directory; those are the automated tests

Step 3:

  • First, write python/src/main.py
  • Open up js/src/main.js and python/src/main.py side by side, and translate the code line-by-line
    • You should stay as faithful as you can to the original Javascript, and try to figure out the exact Python equivalent code
  • You haven't learned about functions or importing modules in Python yet, but those parts have already been stubbed out for you
    • Fill out the addressbook variable by using what you know about Python lists and dictionaries
    • Implement the menu() function -- replace the word pass with your code. (pass is a valid line of Python code that does... absolutely nothing)
    • Implement the main() function -- again replace the word pass with your code
      • You'll have to call the menu() function from within the main() function. How do you call a function in Python? Exactly the same as in Javascript!
  • To check that you wrote up python/src/main.py correctly:
    • Navigate to the python directory in your terminal
    • Run python test/test_main.py
    • You should see:
% python test/test_main.py
.....
----------------------------------------------------------------------
Ran 5 tests in 0.003s

OK
Hint 1 - User Input

You do not need prompt-sync in Python, as Python already has prompting built in. So these lines do not need to be translated to Python

const promptSync = require('prompt-sync');
const prompt = promptSync({ sigint: true });

How do you prompt a user in Python? That was covered in the Control Flow in Python lesson!

Hint 2 - switch statement in Javascript

In Python, the match statement is the equivalent of the switch statement in Javascript

Step 4:

  • Next, write python/src/contacts.py
  • Open up js/src/contacts.js and python/src/contacts.py side by side, and translate the code line-by-line
    • You should stay as faithful as you can to the original Javascript, and try to figure out the exact Python equivalent code
  • Again, the functions have been stubbed out for you; you only have to implement the body of each function
    • Start with the show_contacts() function
    • Next, implement the add_contact() function
      • You'll need to remember how to add items to a Python list. Consult your class notes from the Python Containers lesson!
    • Finally, implement the delete_contact() function, which is the hardest one (but not that much harder given where you are now in your learning)
      • You'll need to recall how to use Python Ranges -- consult your class notes from the Python Control Flow lesson!
  • To check that you wrote up python/src/contacts.py correctly:
    • Navigate to the python directory in your terminal
    • Run python test/test_contacts.py
    • You should see:
% python test/test_contacts.py
.....
----------------------------------------------------------------------
Ran 5 tests in 0.001s

OK
Hint 1 - User Input

You do not need prompt-sync in Python, as Python already has prompting built in. So these lines do not need to be translated to Python

const promptSync = require('prompt-sync');
const prompt = promptSync({ sigint: true });

How do you prompt a user in Python? That was covered in the Control Flow in Python lesson!
Hint 2 - Cleaning up User Input

Whenever a user is prompted for information, you should always sanitize the input by removing extra spaces before and after. This is accomplished in Javascript using the .trim() method, and in Python the equivalent is the .strip() method.

The End

  • To check that you did everything right, you should:
    • Navigate to the python directory in your terminal
    • Run python -m unittest
    • You should see:
% python -m unittest
..........
----------------------------------------------------------------------
Ran 10 tests in 0.003s

OK

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published