quiz 3.4 notes

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Python includes everything until the closing parentheses, even if it is not on the same line.

# OUTPUT # Display the results for the user, repeating the input values. print("For your weight of " + strWeight + " pounds and height of " + strHeight + " inches, your BMI is " + str(BMI) + " which is " + category + ".")

Create an Outline of the Plan

1. Define the problem precisely. Create a username from the first three letters of first name and first four letters of the last name. 2. Gather data. Ask the user for their first name (firstName) and last name (lastName). 3. Perform any needed calculations or data manipulations. Does the user have at least three characters in the first name and four characters in the last name? 4. If yes, create the username with slices. If not, add numbers as needed. 5. Communicate the results, along with an interpretation as needed. Display the username as output. 6. Check the accuracy of your calculations and data manipulations. Test the program with users having long names and short names.

What Grade Do I Need? In a math class, the students take five tests. The average of those five tests is the course grade. The course grade needs to be at least 90 in order to earn an A for the course. Plan a program that will ask students for their first four grades and then calculate the grade needed on the fifth test in order for the average to be at least 90.

1. Define the problem precisely. Find the grade needed on the fifth test in order for the average of five scores to be at least 90. 2. Gather data. Ask the student for the grades on the first four tests. 3. Perform any needed calculations or data manipulations. Calculate the needed grade on the fifth test. 4. Communicate the results, along with an interpretation as needed. Display the needed grade for the user. 5. Check the accuracy of your calculations and data manipulations. Test several sets of grades. To check accuracy, take the user's first four grades together with the predicted grade on the fifth test to see if the average is 90.

Design Steps Input-Process-Output model

1. Define the problem precisely. 2. Gather data. (Input) 3. Perform any needed calculations or data manipulations. (Process) 4. Communicate the results, along with an interpretation as needed. (Output) 5. Check the accuracy of your calculations and data manipulations.

PLAN BEFORE YOU CODE

BMI weight in pounds/ (height in inches)^2 BMI Weight Category Below 18.5 Underweight 18.5 to 24.9 Normal 25 to 29.9 Overweight 30 to 39.9 Obese 40 and above Morbidly Obese ​# A program to find BMI # INPUT # Get the user's weight in pounds. # Get the user's height in inches. # PROCESS # Calculate the BMI. # Determine the weight category. # OUTPUT # Display the results for the user, repeating the input values.

Write a recursive function fib to find the nth Fibonacci number.

For instance, fib(6) would be eight.

RECURSIVE FACTORIAL

Recursion: is the process of defining something in terms of itself. In mathematics, recursion is used with a sequence of numbers. When you define a sequence using recursion, each number is defined in terms of the previous number. 3, 8, 13, 18, 23, 28, and so on. recursive definition The first number is three. For the rest of the list, add five to the previous number in the list to get the next number.

Fibonacci numbers is usually defined recursively.

This version is slightly modified because it starts with two ones. The list starts out usually with two ones. 1,1 After that, each new number is the sum of the previous two numbers. The sum of one and one is two. 1, 1, 2 The next number is 1 + 2, which is 3. 1, 1, 2, 3 The next number is 2 + 3, which is 5. 1, 1, 2, 3, 5 This keeps going forever. 1, 1, 2, 3, 5, 8, 13, 21, and so on.

Since Python executes the code indented after the first true condition, you do not need to add the lower boundary of 18.5.

To read the first "elif" line, the BMI had to be more than 18.5. ​# Determine the weight category. if BMI < 18.5: category = "underweight" elif BMI <= 24.9: category = "normal" elif BMI <= 39.9: category = "overweight" else: category = "morbidly obese"

FACTORIAL CALCULATION

factorial: A factorial of a positive integer is the product of that integer times each positive integer smaller than itself. The symbol used for factorial is an exclamation point. The expression 5! is read as "five factorial" and equals 120. 5! = 5*4*3*2*1=120

Be sure you add enough of an explanation so your user knows what that information represents.

not: 23.5 normal

In programming,

recursion occurs when a function calls itself.

Check the Accuracy of Your Calculations

test data that will give you a result in each weight category and at each boundary. You can search online for a BMI calculator to make testing go faster.

If the user is likely to enter decimal values,

use float() to prevent errors caused by trying to convert a decimal to an int.

turn string version of the user's input into a float will prevent errors

weight = str(110.5) ​# A program to find BMI # INPUT # Get the user's weight in pounds. strWeight = input("What is your weight in pounds? ") weight = float(strWeight)

An alternative output is to use two print() statements, one to repeat the user's input and one to share the result.

​# Display the results for the user, repeating the input values. print("Your weight is " + strWeight + " pounds your height is " + strHeight + " inches.") print("Your BMI is " + str(BMI) + " which is " + category + ".")

BMI program

​# INPUT # Get the user's weight in pounds. strWeight = input("What is your weight in pounds? ") weight = float(strWeight) # Get the user's height in inches. strHeight = input ("What is your height in inches? (1 ft = 12 in) ") height = float(strHeight) # PROCESS # Calculate the BMI. BMI = 703 * weight / height ** 2 BMI = round(BMI, 1) # Determine the weight category. if BMI < 18.5: category = "underweight" elif BMI <= 24.9: category = "normal" elif BMI <= 39.9: category = "overweight" else: category = "morbidly obese" # OUTPUT (alternative version) # Display the results for the user, repeating the input values. print("Your weight is " + strWeight + " pounds your height is " + strHeight + " inches.") print("Your BMI is " + str(BMI) + " which is " + category + ".")

dividing by a large number, rounding to one decimal place will improve the quality of your output.

​# PROCESS # Calculate the BMI. BMI = 703 * weight / height ** 2 BMI = round(BMI, 1) python uses pemdas, so no parentheses are needed in the calculation of BMI. (optional)

A Python Program to Calculate Factorials Add some error handling to your program to stop the user who enters a decimal number, zero, or a negative number. when entering a negative or zero output is 1

​def factorial(number): product = 1 while number > 0: product = product * number number = number - 1 return product strNum = input("Enter a positive integer: ") num = int(strNum) print(factorial(num))

recursively find the factorial of a number

​def recursiveFactorial(number): if number > 1: return number * recursiveFactorial(number - 1) else: return 1 stringNum = input("Enter a positive integer: ") num = int(stringNum) print(recursiveFactorial(num)) Test the program with other values.


Kaugnay na mga set ng pag-aaral

Chapter 5: Mutual Assent of the Parties

View Set

Chapter 6 - Digital Marketing, Chapter 5: Paid Search Marketing, Quiz 7, Digital Marketing - Chapter 8, Chapter 9, Stukent Chapter 7, Chp 8-13

View Set

Ch. 12: Principles of Pharmacology

View Set

Unit 6 Migrations Test Study Guide

View Set

Chapter 46: Nursing Care of the Child With an Alteration in Cellular Regulation/Hematologic or Neoplastic Disorder

View Set