MTEC1003 Media Computation Skills Lab

Fall 2023 Wed 6:00-8:30 [ONLINE] Prof. Louis Goldford.

SYLLABUS SCHEDULE SOFTWARE + RESOURCES GRADING

Lab 10 / Part 1: While Loops in Python + JavaScript

C O N T E N T S

Setup for Python3
Setting Up Your Lab 10 Repository

  1. Gimmy My Number
  2. Which Floor Do You Work On?

Setup for Python3

Let’s be sure your Terminal session will run Python3 (instead of the prepackaged Python 2.7.x) when calling python on the command line. We can crete an “alias” for python3 by running the following command:

  $ alias python=/usr/local/bin/python3 

Alternatively, you can simply run python3 in the Terminal instead of python, but this gets annoying after awhile.


Setting Up Your Lab 10 Repository

ProTip: Bookmark the following page so that you can easily repeat these steps in your future labs!


1. Gimmy My Number

  1. Using your text editor, create a new file called gimmymynumber.html in your lab-10-more-loops repository.
  2. Set up an HTML file, and add <script> tags… start writing your JavaScript between the tags.
  3. Ask the user for a number greater than 100 (“Gimmy a number greater than 100 plz…“).
  4. If the user enters a number greater than 100, congratulate them! (<number> is greater than 100! Good work.”).
  5. But if they enter a number less than 100, ask again (“This number is less than 100, dummy. Try again… I’ve got all day…”).
  6. Use a while loop to accomplish this. Your loop should keep asking the user to enter a satisfactory number, and it should do so for as many times as necessary; until finally the user returns a number greater than 100.

Example JavaScript Console output should resemble the following:

Gimmy Example Output


2. Which Floor Do You Work On?

  1. Using your text editor, create a new file called whichfloor.html in your lab-10-more-loops repository.
  2. Set up an HTML file, and add <script> tags… start writing your JavaScript between the tags.
  3. Declare a variable called maximum_stories and initialize it with a value of your choice. In other words: Decide how many floors your building should have.
  4. Ask the user which floor they work on (“On what floor of the building is your office?”).
  5. If the user enters a number greater than maximum_stories, format + print an error message telling the user:
    • what number they entered,
    • how many floors are in the building, and
    • ask them to enter a valid number.
  6. Print this same message to the JavaScript Console; so that we we can keep a record of what the user entered.
  7. If the user enters a valid number, format + print some kind of “Congratulations!” message.
  8. Use a while loop to accomplish this. Your loop should keep asking the user to enter a satisfactory number and should do so for as many times as necessary; until finally the user returns a number within the bounds of your maximum_stories variable.
  9. Save your HMTL/JavaScript file, and make sure to test it using the JavaScript Console.
  10. Try initializing your program with different values for your maximum_stories variable. For example, if you original set maximum_stories to a value of 10, increase it to 20 or some other value of your choice. Change this number at the start of your JavaScript program, save your HTML file in the text editor and refresh your program in Chrome. Test to make sure that your new values are reflected in the error messages thrown by your while loop program.

Example JavaScript Console output should resemble the following:

Stories Example Output

Make sure all your files are included in your lab-10-more-loops repo folder, and don’t forget to add, commit, and push any additional changes! Use your git cheatsheet (from lab 3) if you need it!

At the end of Lab 10 / Part 1, your lab-10-more-loops folder should include the following files:

When finished, move on to Lab 10 / Part 2: Python + JavaScript Recursions