Collab1 (Fri 19-Jan)

Collaborative, in-person, in recitation.


Notes:
  1. First read this entire write-up before starting!
  2. Collabs are graded chiefly based on participation and effort, though we may occasionally assign some points to outcomes as well. Unexcused absences receive 0 points. Otherwise, the grading is only full-points, half-points, or no-points. If a TA notices that you are not participating properly, your grade will be lowered accordingly.
  3. While collaboration is only recommended and not required for the rest of this hw, collaboration is required in collabs. Your recitation TAs will organize you into groups. You must work productively the whole time with your partner(s).
  4. If you or your partner(s) already did these exercises, then you should redo them from scratch in Friday collab (without referring to any of your previous work). In the future, we encourage you to not complete Friday collab exercises before attending Friday collab. After all, the goal is not to simply finish the collab exercises, but to collaborate and discuss them with your collab partner(s). If you do not fully complete them during collab, you may optionally complete them afterwards, but your collab grade is based on your participation in Friday recitation.
  5. Do not use loops, strings, lists, or any topics not covered in Unit 1.

missingDigitQuizzer
Your main task today is to write the function missingDigitQuizzer() so that your app closely (if perhaps not perfectly) matches the app in this video:

Also, once you fairly closely match the video, you may go further (time permitting), adding more features or more well-chosen complexity. However, if you have some time, you should first do the "Time Permitting" exercise described below.

Suggested Starter Code:
import random

def twoDigitRandomNumberWithoutZeros():
    # We found this to be a useful helper function
    return 42 # replace this with your code

def missingDigitQuizzer(operator):
    return 42 # replace this with your code

def runMissingDigitQuizzer():
    score = 0
    score += missingDigitQuizzer('+')
    score += missingDigitQuizzer('-')
    score += missingDigitQuizzer('*')
    print(f'Your total score: {score}/3')

def main():
    runMissingDigitQuizzer()

main()

Important Hints:
  1. Again, do not use loops, strings, lists, or any topics not covered in Unit 1.
  2. For this exercise, you can use the CS Academy Sandbox (preferred) or you can run a Python file outside of CS Academy (acceptable).
  3. Use only two digit numbers, at least at first. You can explore beyond that for an additional feature after you finish the app.
  4. Actually, we suggest that you use two-digit numbers that do not contain any 0's, as this simplifies the problem a bit. You can write a helper function that generates these, perhaps by generating random digits in the range [1,9] (inclusive) and then combining these digits into a two-digit number.
  5. To generate a random integer between lo (inclusive) and hi (exclusive), use random.randrange(lo, hi). For example, random.randrange(3, 6) returns a random integer in the range [3, 5] (inclusive).
  6. For subtraction (x - y), we guaranteed the answer is non-negative (and you may want to do this, too!). To do this, if the randomly-generated x is smaller than the randomly-generated y, we swapped the values.
  7. To keep things easier, we always made d (the hidden digit) be the first digit in the second number. You can do this, too, if you wish, though you might think about how to be more general than that.
  8. For information on how to get user input, revisit 1.3.7 Console IO Functions in CMU CS Academy.
  9. While you will have to use strings (like 'You got it!!!'), do not use string functions (like len(s)), methods (like s.upper()) or slicing (like s[i] or s[i:j]).
  10. In fact, you will want to use f strings to include values of variables in the string, like so:
    print('This shows how to use f-strings to print variable values:')
    x = 123
    print(f'x = {x}') # prints: x = 123
  11. First make the app work without proper right-alignment. Then add right-alignment.
  12. You can use f strings to right-align, like so:
    print('This shows how to right-align with f strings:')
    x = 123
    y = 45
    print(f'x = {x:>4}')
    print(f'y = {y:>4}')
    print('See how the values for x and y are right-aligned')
    print('with a field-width of 4.')

Time permitting:
If you finish missingDigitQuizzer, you and your partner(s) should work together on Additional Code Tracing Exercises from 1.3.12 and 1.4.13.