Appendix A — Assignment 1

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. If your document is not clean and organized, you can lose up to 2 points:

    • 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.
  6. If your document does not include the output of your code, you may lose up to 5 points.

A.1 Question 1 (4 points)

  1. Create a a variable called var_float that contains a decimal number.

  2. Store a sentence as var_sent that reads exactly as follows: “The square of {} is {}.” Where the first {} is your var_float and the second {} is the square of that variable. Print your sentence.

  3. Print the output of using the count method to determine how many spaces are in var_sent.

  4. Round your var_float to 0 decimal places and convert to an integer. Store this as var_int and print the type to verify this was done correctly.

A.2 Question 2 (3 points)

Have a user input 2 Booleans (hint: must be type bool). In a single print line, using only and, or, not functions, have the output return True if both variables are the same, and False is they are different.

Clarification: 1) cannot use conditional if statements 2) this must be capable of printing the correct output for any possible booleans the user could enter, not just the one example that your html will show.

A.3 Question 3 (6 points)

At Northwestern, email addresses are classified as follows:

  • Student email addresses end with @u.northwestern.edu.
  • Professor email addresses end with @northwestern.edu (but not @u.northwestern.edu).

Write a Python program that: 1. Asks the user how many email addresses they will enter. 2. Prompts the user to input each email address. 3. After all email addresses are entered: - Print all professor email addresses under the heading "Professor Emails:". - Print all student email addresses under the heading "Student Emails:". - If no professor or student emails were entered, print "None" under the respective heading.

A.3.1 Requirements:

  • Do not use lists or other advanced data structures, since we have not covered them yet.
  • Use only basic string operations and loops.
  • The program must handle all cases, regardless of uppercase or lowercase in the email addresses.
  • Trim any leading or trailing whitespace in the user input before classifying the email.

Example Run:

How many email addresses will you be entering? 3
Enter an email address: lshi@northwestern.edu
Enter an email address: jackyu@u.northwestern.edu
Enter an email address: Alexa@u.northwestern.edu

Output:

Professor Emails:
lshi@northwestern.edu

Student Emails:
jackyu@u.northwestern.edu
alexa@u.northwestern.edu

A.4 Question 4 (3 points)

Write a tip calculator program that asks the user for the price of the meal and the percent tip they want to leave. Then print a sentence that displays both the tip amount and total bill. Example if meal price is 25 dollars and tip is 15 percent:

                               Your tip amount is $3.75 and your total bill is $28.75.

A.5 Question 5 (3 points)

Write a program that asks the user for a number of seconds and prints out how many minutes and seconds that is. Example:

                                200 seconds is 3 minutes and 20 seconds.
                                           

Use only two lines of code for this question: one line for the input and one line for the print.

A.6 Question 6 (4 points)

Write a program that asks the user to enter two numbers. Have the program return one of the following messages depending on which criteria is met.

num1 is greater than num2”; “num 1 is less than num2”; “num1 is equal to num2”; where num1 and num2 are the user inputed values.

Show the output of the program with any two numbers of your choice.

A.7 Question 7 (4 points)

  1. Use a single if-elif-else statement to print the smallest of 3 user defined numbers. Show the output of the program with any three numbers of your choice.

  2. Use a nested conditional statement to print the smallest of 3 user defined numbers. Show the output of the program with any three numbers of your choice.

A.8 Question 8 (6 points)

Write a program that asks the user to enter either rock, paper, or scissors. Use a conditional statement to determine if the user wins, loses, or ties the computer at the game “Rock, Paper, Scissors”. Note: rock beats scissors; scissors beats paper; paper beats rock

Print a meaningful sentence that includes the winner, the computer’s choice, and the user’s choice.

  • Handle case sensitivity (example: if the user enters Rock, it will still run).
  • If the user enters a word other than one of the choices, print “Invalid choice.”.
  • Show the output of the program when the user enters Rock (capitalized)
# starter code to generate a random choice of rock, paper, scissors
import random as rm
comp_choice = rm.choice(['rock', 'paper', 'scissors'])

# your solution in this code chunk below

A.9 Bonus (6 points)

FFor all questions in this assignment that involve accepting user input:

  • Use a try-except block to handle cases where the user may enter invalid input (e.g., non-numeric values).
  • Implement a loop that repeatedly prompts the user until a valid value is provided, so the program can proceed safely.
  • To receive credit, you must revise each applicable question by modifying your code to include both the input loop and error handling directly in that question’s solution.

Each revised question is worth 1 point.