Program of the Day #2

In POTD #1, you wrote a multiplication quiz and you chose the question. Now, let’s make a new version of the math quiz program where the user gets to specify the numbers. The program should take two command line arguments, representing the first and second integers to be multiplied. The user then has a chance to enter their answer, before you print out the correct answer. A sample run of the program might look like the following:

>>> %Run P02_quiz2.py 10 4
What is 10 * 4 ?
40
10 * 4 is 40

An example run where the user didn’t type any answer when asked for input (but just hit enter) would look like this:

>>> %Run P02_quiz2.py 30 2
What is 30 * 2 ?

30 * 2 is 60

Note: When showing example runs of programs that take program arguments, the first line of the transcript shows what’s displayed as a result of pressing Thonny’s Friendly Green Run Button with the given arguments in the Program Arguments box. In the above example, the contents of the Program arguments box is 10 4.

Other Practice Problems

  1. Suppose that you are writing a program and you have two variables x and y that contain values. You’d like to swap the values stored in those variables, so what was previously stored in x is now stored in y and what was stored in y is now in x. For example if the following program is completed with your code, it should output 4 10. Note: your solution should not depend on the specific values (e.g., 4 and 10) - it should work regardless of what x and y are set to.

    x = 10
    y = 4
    # your code here
    print(x, y)