Lab 1

Directions for Labs


  • Submitting your completed lab file online to CourSys (not Canvas). Labs are marked on completion, not correctness, so complete each part to the best of your ability and learn!
  • It is recommended that students attend in-person lab sections for lots of help!
    • If you would like to attend an in-person lab section, you are welcome to come to any (or more than one) lab section.
    • There is no need to attend the section you are enrolled in.
    • There is no need to attend any lab sections: there is no attendance taken.
    • You can complete the lab on your own time or own computer if you like.
  • While completing these labs, you are encouraged to help your classmates and receive as much help as you like. Assignments, however, are individual work and you must not work with another person on assignments.

You may not work on assignments if you are in the CSIL lab during CMPT 120 tutorial times.

1. Inspirational Quote


  1. Open VS Code (or the Python IDE of your choice) and your cmpt120 folder:
    • (Recommended) Install VS Code and Python on your computer [video].
    • (Recommended) Or, use an SFU computer in the CSIL lab [guide].
    • If you don't want to install anything on your computer, follow this guide on remotely connecting to an SFU server through your browser (via VDI).
    • If you are unable to connect to SFU, you can use an online website to write your code; however, this is not a recommended long term solution. Please ask a TA or the instructor for help
  2. Inside your cmpt120 folder create a new folder named lab1.
  3. Inside your lab1 folder, create a new file name lab1.py.
  4. Make your lab1.py print to the screen an inspirational quote.
    • Print to the screen the quote, along with who said it.
    • No requirements about formatting; just a chance to start using the print() statement.
  5. Run your program to ensure that it works!

2. Advice Chatbot


Pick a fictional personality who can give (good? bad?) advice, such as from a movie, a book or a fairy tale. Make it fun! You are going to create a chatbot that speaks in their "voice". Need some ideas: Big Bad Wolf, Fairy Godmother, HAL 9000, Ironman, Jarvis, Dobby, Snape, Glinda, Rocky, Wicked Witch of the West, ...

  1. After the inspiration quote from above, print a greeting message from your chosen personality.
  2. Ask the user for their name, and then have your personality greet the user by name, such as "Pleased to make your acquaintance, Brian."
    • Remember that input() reads from the keyboard:
    • You'll need to store the user's name in a variable so you can use it later.
    • Remember that you can put a + between two strings to concatenate them together.
  3. Tell the user which direction to travel to seek their fortune:
    • Create a list of four strings; one for each direction (North, East, ...)
    • Recall that a list might look like ["apple", "bean", "cherry"].
    • Randomly pick one of the directions from the list.
      • Hint: use random.choice() on a list to pick one.
      • Remember to import random.
    • Tell the user that their fortune awaits in that randomly chosen direction.
      • Hint: When you use random.choice() on you list of directions, store that direction in a variable.
      • Concatenate that direction with a message and print it to the user.
    • Recall the lecture notes included an example that used random selection from a list.
  4. Ask the user a yes/no question and print out a unique message in either case.
    • Put the input() statement before the if statement to get the user's input and store it in a variable.
    • Use an if-else statement to decide which of the messages to print out.
    • Have the if statement check the user's input to see if it equals (==) "yes"; don't worry about handling upper case vs lower case.
    • You'll need one print statement for when the if statement is true, and another one in the else for when it is false.
  5. Change your if statement to handle not only yes/no, but also yup/nope:
    • Currently, your if statement likely checks for one word, and uses an else to handle the other case.
    • Change the if statement to check if the user's input equals "yup" in addition to "yes".
    • To check if the input equals one of two things, you'll need to use an or.
    • Recall lecture uses an example with or.
    • One statement from the in-class example is:
      if reply == "Good" or reply == "good" or reply == "great":
      • It is checking for different words and different capitalizations of a word. In this lab you only need to check for two words ("yes"/"yup"; or "no"/"nope"), so it will be a little simpler.
      • Change this statement to check for "yes" and "yup".
      • Notice that this statement repeats the variable name reply when checking it for different values.
    • The structure you'll end up with is (assuming the variable name is myInput):
      if myInput == "..." or myInput == "...":
          print(...)
      else:
          print(...)
    • (Optional) Once you have it working with or, experiment by changing to and. Does it work? Can you explain to yourself why? (Hint: You'll need to use or).
  6. Run your program a few times to ensure that:
    • It generates random directions correctly:
      • Each run it should be randomly picking between "North", "East", ....
      • When you run your program a few times expect to see different directions each time. Since it's random you may see the same direction a couple times in a row just like if you were flipping a coin.
    • It does the right thing when you type in "yup", "yes", "nope", or "no".
      • Run the program once and test for "yes". Ensure it shows you the correct output.
      • Run it again with input "yup"; it should give the same output.
      • Run again for "no", then again for "nope".
    • What happens when you run it and enter "oops"? Can you explain why? (You don't have to make it work correctly for inputs such as "oops", but you are welcome to do so!).
  7. (Optional) You are welcome to expand on the above steps! Have fun!
    • You are welcome to add more to your program, making it more complicated!
    • Your final submission should still meet the above required behaviour for the inputs "yes", "yup", "no", and "nope"; any additional behaviour is great!
    • Maybe try making it handle other cases such as:
      • "oops"
      • "No" (capital N)
      • "NO" (all capitals)
    • Maybe try making it ask the user another question. Do you want it to be a yes/no question? How about open ended like "What is your goal in life?" Can your personality give fun advice?
    • Experiment with other things covered in lecture, such as an if-elif-else statement.

Submission


Submit your lab1.py file to CourSys by Sunday midnight. Remember you can submit up to 3 days late with no penalty if needed (but please plan to get it done on time to stay up to date on the course!)