Homework 4 – Conditions and Loops solution

$25.00

Original Work ?
Category: You will Instantly receive a download link for .ZIP solution file upon Payment

Description

5/5 - (4 votes)

Instructions: Solve problems 1 and 2 on paper or doc file (no separate scripts required). For problems 3-8 write
Python scripts (.py files). The code and output screenshot should also be present in the doc file. Use variables where
necessary and give meaningful names to variables. All script files should have a comment block at the top, and also
provide comments alongside your code. Upload a .pdf/.docx file containing your solutions along with your .py files.
If you solve on paper, please upload a good quality scan using CamScanner/Office Lens/iScanner.
Problem 1. Analyze the following code. If you (hypothetically) check the condition count < 100 at the positions in the code designated as # Point A, # Point B, and # Point C, will the condition evaluate to always True, always False, or sometimes True and sometimes False? Give your answer for all the three positions. [10 points] count = 0 while count < 100: # Point A print("Programming is fun!") count += 1 # Point B # Point C Problem 2. How many times does the body of the while loop repeat? What is the output of each loop? [10 points] i = 1 while i < 10: if i % 2 == 0: print(i) i += 1 Problem 3. Write a python script that prompts the user to enter an integer and checks whether the number is divisible by both 5 and 6, divisible by either 5 or 6 (not both), or not divisible by 5 and 6. [15 points] Sample program output I: Enter an integer: 10 10 is divisible by either 5 or 6 Sample program output II: Enter an integer: 30 30 is divisible by both 5 and 6 Sample program output III: Enter an integer: 14 14 is not divisible by either 5 or 6 CMPSC 101 Instructor: Dr. Mahfuza Farooque Introduction to Programming in Python Fall 2016 Problem 4. Write a python script to print the following table to display the sin value and cos value of degrees from 0 to 360 with increments of 10 degrees (the table displayed below has been shortened for space reasons, however your program should generate the complete table). Round the valuesto four decimal point precision and format the columns so that they are nicely aligned. Do not use any looping structure other than while. [Hint: Use the trigonometric functions in the math module to calculate the sin and cos values.] [25 points] Deg. Sin Cos 0 0.0000 1.0000 10 0.1736 0.9848 … 350 -0.1736 0.9848 360 0.0000 1.0000 Problem 5. Write a python script that prompts the user to enter the month number and year and displays the number of days in the month. You need to do the proper leap year calculations based on the year. [25 points] Sample program output I: Enter month: 3 Enter year: 2005 March 2005 has 31 days Sample program output II: Enter month: 2 Enter year: 2000 February 2000 has 29 days Problem 6. Write a python script that prompts the user to enter the number of students and each student’s score, and displays the highest score along with the student’s name. Do not use any looping structure other than while. [25 points] Sample program output: Enter number of students: 3 Enter student #1 name: Peter Enter student #1 score: 90 Enter student #2 name: Janet Enter student #2 score: 98 Enter student #3 name: Jack Enter student #3 score: 85 Top student: Janet Score: 98 CMPSC 101 Instructor: Dr. Mahfuza Farooque Introduction to Programming in Python Fall 2016 Problem 7. Write a python script that lets the user guess whether a randomly flipped coin displays head or tail. The program randomly generates an integer 0 or 1, which represents head or tail respectively. The program prompts the user to enter a guess value (0 or 1) and reports whether the guess is correct or incorrect. [25 points] Sample program output I (computer guesses Tail): Enter 0 for Head and 1 for Tail: 0 Sorry, it is a tail. Sample program output II (computer guesses Head): Enter 0 for Head and 1 for Tail: 0 You guessed correctly! Sample program output III (computer guesses Head): Enter 0 for Head and 1 for Tail: 1 Sorry, it is a head. Problem 8. Write a python script to calculates a person’s body mass index (BMI). The BMI is often used to determine whether a person is overweight or underweight for his or her height. BMI is calculated with the following formula: ??? = ????ℎ? (ℎ???ℎ?) 2 × 703 Weight is measured in pounds (lb) and height is measured in inches (in). The program should ask the user to enter his or her weight and height and then display the user’s BMI. The program should also display a message indicating whether the person has optimal weight, is underweight, or is overweight based on the given table. [15 points] BMI 25 Overweight 18.5 <= BMI <= 25 Normal BMI < 18.5 Underweight Sample program output: Enter weight (lb): 134 Enter height (in): 66 BMI: 21.62 You are normal.