Appendix B — Assignment 2

STAT 201

Author

YOUR NAME

Instructions

  1. Write your name on the assignment.

  2. Write your code in the Code cells of the template provided to write solutions for the assignment. Do not open a new notebook, and work from scratch. Ensure that the solution is written neatly enough to understand and grade.

  3. Use Quarto to print the .ipynb file as HTML. You will need to open the command prompt, navigate to the directory containing the file, and use the command: quarto render filename.ipynb --to html. Submit the HTML file.

  4. You may talk to a friend, discuss the questions and potential directions for solving them. However, you need to write your own solutions and code separately, and not as a group activity. Do not use AI to solve the problems.

  5. There are 2 points for cleanliness and organization. The breakdown is as follows:

    • Must be an HTML file rendered using Quarto.
    • There aren’t excessively long outputs of extraneous information (e.g. no printouts of unnecessary results without good reason, there aren’t long printouts of which iteration a loop is on, there aren’t long sections of commented-out code, etc.). There is no piece of unnecessary / redundant code, and no unnecessary / redundant text
    • The code follows the python style guide for naming variables, spaces, indentation, etc.
    • The code should be commented and clearly written with intuitive variable names. For example, use variable names such as number_input, factor, hours, instead of a,b,xyz, etc.

B.0.1 Question 1 (3 points)

  1. Write a program that counts down from 5 and then prints the message “Go”.

  2. Write a program that asks the user for an integer, then counts down from that integer and then prints the message “Go”. Use a try-except to ensure the user entered an integer. If it is not an integer, return the message “You must enter an integer”. Show the output of the program if the user inputs 3.

  3. Copy and modify part b to allow the user to keep entering input until they correctly enter an integer (ie: don’t end after the except). Show the output of the program if the user first inputs 8.9 and then inputs 7.

B.0.2 Question 2 (2 points)

Print a star formation as follows:

                                         * 
                                         * * 
                                         * * * 
                                         * * * * 
                                         

Ask the user to input the height of the formation first. For example, 4 should return the formation above. Show the output of the program if the user inputs 5.

B.0.3 Question 3 (6 points)

  1. Write a program where you play 3 “Rock, Paper, Scissors” games against the computer. Simulate a random choice for the computer each game and ask the user to enter either rock, paper, or scissors. Print the number of times the user won out of 3 games. Show the output of the program after playing the game.

Recall: rock beats scissors; scissors beats paper; paper beats rock.

Example:

                                "You won <num_win> out of 3 games."
# import the random function and name the alias rm

# code to generate a random choice of rock, paper, scissors for the computer
comp_choice = rm.choice(['rock', 'paper', 'scissors'])
  1. Write a program where you play “Rock, Paper, Scissors” games against the computer until you have won 3 games. Simulate a random choice for the computer each game and ask the user to enter either rock, paper, or scissors. Print the number of games it took to win 3 times. Show the output of the program after playing the game.

Example:

                                "You won 3 out of <num_games> games."

B.0.4 Question 4 (6 points)

  1. Write a program (loop) that prints all the factors of a positive integer input by the user. A factor is any positive integer that divides the number and leaves no remainder. Show the output of the program if the user inputs 24.

Example: The factors of 8 are 1, 2, 4, 8.

  1. Write a function called number_of_factors that takes an integer and returns how many factors the number has. Run your function with 24 to check if it works.

B.0.5 Question 5 (6 points)

  1. Write a program (loop) that identifies whether a positive integer input by the user is prime or not. A prime number is a number whose only divisors are 1 and itself. Show the output when the program is used to check if 89 is prime or not.

  2. Write a function that checks if a positive integer is prime or not. It should take one integer input and return a boolean. Run your function with 197 to check if it works.

B.0.6 Question 6 (4 points)

Write a function that calculates the area of a rectangle. The function should have an input for length (in inches) and width (in inches). And have 2 ouputs: the area in terms of square inches and the area in terms of square feet. There are 12 inches in 1 foot. Run your function with a length of 102 inches and width of 60 inches to check if it works.

B.0.7 Question 7 (3 points)

Write a function that takes a word and a sentence as two string inputs and returns the number of times the word occurs in the sentence as the output.

Run your function with “sea” and “Sea shells are on the sea shore when the sea is calm.” inputs to check if it works. Your function should work for any word and sentence.

Note: at this time it does not have to differentiate a distinct word just if it appears in any form. Example: “The sea gives me nausea.” would return 2.