Python Questions

User Generated

entunq2010

Programming

Description

Unformatted Attachment Preview

CHAPTER 5 Selection and Repetition Assume: a=10 and b = 20 GEORGE MASON UNIVERSITY GEORGE MASON UNIVERSITY Assume: a=10 and b = 20 GEORGE MASON UNIVERSITY Assume: a=10 and b = 20 GEORGE MASON UNIVERSITY GEORGE MASON UNIVERSITY • Expression with value: • True or False GEORGE MASON UNIVERSITY • Variable with value: • True or False GEORGE MASON UNIVERSITY Sequence Selection Repetition Module Execute a list of statements in order Choose at most one action from alternative conditions Repeat a block of statements while a condition is true Function: set of statements GEORGE MASON UNIVERSITY IF STATEMENT (SELECTION) Determine whether or not to execute a statement (which can be a single statement or an entire block) TRUE statement expression FALSE GEORGE MASON UNIVERSITY if x > 0: y = sqrt(x) if x > y: t=x x=y y=t GEORGE MASON UNIVERSITY IF .. ELSE PROVIDES TWO-WAY SELECTION TRUE if clause expression FALSE else clause GEORGE MASON UNIVERSITY Output larger of two numbers GEORGE MASON UNIVERSITY Don't allow sqrt of negative number Use sqrt function from math library GEORGE MASON UNIVERSITY Determining even or odd GEORGE MASON UNIVERSITY Give student another chance at a question else : print (“Incorrect”) #ask again GEORGE MASON UNIVERSITY • • and, or : binary operators not : unary • and or not • used to combine boolean values to produce a boolean result • truth tables operator precedence not then and then or not, and, or are very low, below other operators GEORGE MASON UNIVERSITY True and False are values for Boolean variables True or False are values for Boolean expressions GEORGE MASON UNIVERSITY GEORGE MASON UNIVERSITY GEORGE MASON UNIVERSITY GEORGE MASON UNIVERSITY GEORGE MASON UNIVERSITY Enter this program into IDLE, save. Run and test with a variety of input GEORGE MASON UNIVERSITY SELECTION STATEMENTS PYTHON Chapter 5 • • Condition: Relational Expression that evaluates to true or false Operands: numbers or strings Relational Relational Operator == Description is equal to Operators are binary != < >= is greater than is greater than or equal to is char or substring in in GEORGE MASON UNIVERSITY Logical Operators and or not Operands are boolean expressions Binary operators: and or Unary operator: not GEORGE MASON UNIVERSITY if condition : statement A colon (:) indicates a if condition : statement1 statement2 … The condition may be in compound statement parentheses – not required The statement block begins on the line following the header and is a group of one or more statements, all of which indented to same level – any number of spaces GEORGE MASON UNIVERSITY if condition : statement else otherStatement indentation level must be the same for if and else Use tab or space – don’t mix! GEORGE MASON UNIVERSITY Notice indentation! Statement can be anything including another if elif can be used Instead of else : if GEORGE MASON UNIVERSITY GEORGE MASON UNIVERSITY The function statements may be written: if n % 2 == 0 : return true else return false The result of the function call is Boolean GEORGE MASON UNIVERSITY p and q p or q Python first evaluates p. If p is false, the result is false and q is not evaluated Python first evaluates p. If p is true, the result is true and q is not evaluated Improves efficiency Usually can ignore – just keep in mind may cause a bug GEORGE MASON UNIVERSITY ITERATIONS IN PYTHON Chapter 5 >>> is an Expression prompt Expressions generally fit on one line. Using a parenthesis allows you to continue on another physical line GEORGE MASON UNIVERSITY variable is defined first time it is assigned a value expression that replaces the previous value datatype is associated with the value stored not the variable variable must be created and assigned before it can be used in expression Same variable name on both sides of = previously defined variables replaced by their values in computation GEORGE MASON UNIVERSITY • • • Built-in (min, round, …) defined arguments GEORGE MASON UNIVERSITY Arithmetic operations for numbers only Convert String to int/float for calculations Concat for Strings only Convert number to String GEORGE MASON UNIVERSITY GEORGE MASON UNIVERSITY • Arithmetic • • • • +, -, *, / // integer div – floor % ** • String • + to concat string GEORGE MASON UNIVERSITY output multiple ‘things’ – constants, values of variables, values of expressions – by separating with a comma Line break: \n Repeat string in output Multiple lines: one statement GEORGE MASON UNIVERSITY • • • • function: input parameter: string (prompt) result: String value entered by user DO something with the result! GEORGE MASON UNIVERSITY Select: File…New Enter these lines of code into the Python Source File GEORGE MASON UNIVERSITY Enter Code into file window and SAVE Hit F5 (or select Run..Run Module) Result GEORGE MASON UNIVERSITY • while : … • condition • • • compare quantities , =, ==, != and, or, not GEORGE MASON UNIVERSITY Create new file User input for deposit amount and current rate Convert input to numbers for computation GEORGE MASON UNIVERSITY Range of values known (e.g., number of years) GEORGE MASON UNIVERSITY ALGORITHMS AND PYTHON Chapter 5 • Make a plan • Limited to specific actions: • input a value, output, arithmetic operations, yes/no questions, repetition • Used to organize a plan with those limited resources • Flowcharts BEFORE code! GEORGE MASON UNIVERSITY • Problem: Input two values and find the largest • Remember: input, output, arithmetic operations, yes/no questions, repetition • We will go flowchart methods GEORGE MASON UNIVERSITY start input x input y no max = y yes x > y? max = x output max stop GEORGE MASON UNIVERSITY GEORGE MASON UNIVERSITY GEORGE MASON UNIVERSITY GEORGE MASON UNIVERSITY Problem: count the number of even numbers between two user inputted values that are both even and a multiple of 5 First: figure out how you would describe solving the problem in ENGLISH. Using only your limited resources GEORGE MASON UNIVERSITY GEORGE MASON UNIVERSITY Process a savings club account, only allowing deposits GEORGE MASON UNIVERSITY GEORGE MASON UNIVERSITY Design an algorithm and then create the Python code to solve the indicated problem. At the end of each month, after paying her bills and putting money into savings, Maria wants to make donations to her favorite charities. Prepare an application for her to enter her net salary and total bills. Then allow her to enter donations until her excess funds are depleted and she wants to continue donations. Finally, print the total amount she donated. GEORGE MASON UNIVERSITY Maria’s Budget Input budget info Input salary Input bills Output total donated Process donations Calculate remaining Input amount Add amount to total For each desired, allowable donation GEORGE MASON UNIVERSITY Start Input salary Input bills remain = salary - bills Input donate? donate == ‘yes’ and remain > 0 n Output total Stop y Input amount amount > remain y Input amount n total += amount remain -= amount Input donate? GEORGE MASON UNIVERSITY GEORGE MASON UNIVERSITY GEORGE MASON UNIVERSITY GEORGE MASON UNIVERSITY IMPROVEMENT GEORGE MASON UNIVERSITY Xavier McMillon IT 102 – DL1 Question 1 1. Create a flow chart for the following: Ask the user their name. input how many hours it has been since the user has eaten. Make sure the input is >= 0. Ask the user if they are hungry. If yes print “Go eat”, if no print “continue with your day” Xavier McMillon IT 102 – DL1 Question 2 2. Create a python program for the following flowchart Start Ask the user if they like sports No Print “Oh well” If the sport is not on the list add it to the list using “.extend” and print the name of that sport is fun YES Ask what their favorite sport is If the sport is on a predetermined list print the name of that sport is fun s3.us-east-1.amazonaws.com 16 Bb https://s3.us-east-1.amazonaws.com/blackboard.learn.xythos.prod/5a3o... SOLUTION: python question - Python Homework Help - Studypool Untitled + IT102 Chapter 5 Homework Homework Instructions: Timing: Submit your homework to Bb by 11:30pm of the day it is due in order to ensure that it has passed through Bb's gates and is ready to process before the end of the day. The last document you submit on time is the one Bb will grade. NO LATE HOMEWORK IS ACCEPTED FOR ANY REASON NOTE: If some documented medical situation interferes with your ability to get your work in on time, notify your instructor. Late homework is never graded; however, your instructor needs to know about your circumstances so that he/she can work with you to find the best options. Instructions for the layout of your Homework Document: You are asked to draw flowcharts, create Python program files, run programs, and obtain output in the IDLE window. Photos and/or screenshots of this work must be included in your HW document. Please ensure that everything is legible. The final document must be in PDF format. If you do not know how to create a PDF document, call or visit GMU'S ITS Support or a VSE lab with a student assistant. Show ALL of your work in order to get credit. There is a Power Point example of a HW submission on Bb under "Help with Course Materials" on the "Syllabus and Course Information site For these problems, 1. Write a Python program to: ask the user to enter two integers: inti and int2. The program uses the exponential operator to calculate and then print the result when int1 is raised the int2 power. You also want to calculate the result when int1 is raised to the 5 power; however, you realize that it is not possible to take the square root of a negative number. If the value for int that is entered is a negative number, print a message to the user explaining why you cannot complete the task. Otherwise, calculate the square root and print it. Finish the program by printing the values of int1 and int2. 2. For problem #1, when the user entered a negative number, you output a message that it was not possible to take the square root of a negative number. This time you will write a Python program that asks the user to enter a non-negative number (float). If the user does enter a negative number, you will use a while loop to continue prompting the user until you get a valid number i.e. a number >= 0). Once the user enters a valid number, you calculate and print the square root. Test your program twice, once using a negative number and once using a non-negative number. 1 mymasonportal.gmu.edu Bb https://s3.us-east-1.amazonaws.com/blackboard.learn.xythos.prod/5a3o... SOLUTION: python question - Python Homework Help - Studypool Untitled + 0 0 0 Chapter 5 Discussion Board Assignment As per the syllabus, the discussion board is a graded activity. There are two parts for this assignment: 1. You are to submit two questions that might be seen on an exam from the material in the chapter (use the practice problems for guidance). You may either create an original question or find one using any resource (e.g., website, book). Any resource used must be properly cited or that constitutes plagiarism. NOTE: if you include a question that is in our text or on this Blackboard site - you will receive no credit for that submission. 2. You are to completely answer two questions posed by other students. If you use a resource (other than our text or material on this Blackboard site), it must be properly cited. NOTE: if you respond to a student question that has already been answered - you will receive no credit for that submission. When responding to other students (part 2 of this assignment), you are REQUIRED to copy/paste the original posed question, failure to do so will result in a score of zero. Please do not just respond to the thread. Your grade for the discussion is out of a possible 25 points. The quality of your posed questions determines your score for the first part and the accuracy/completeness of your responses determines your score for the second part. You are REQUIRED to create FOUR different threads. EACH of those threads must include an attached PDF file. Your files MUST be named as follows. For the questions you are posing: lastNameQ1.pdf, lastNameQ2.pdf. For the questions you are responding to: lastNameResponse1.pdf, lastNameResponse2.pdf. In your response PDF files - you are REQUIRED to include the original question. For Chapter 5 Discussion Board Assignment: Your posed questions should include one of each type of question: Request creation of a flowchart to model a given application - this must include a decision and a loop; Provide a flowchart that models a DIFFERENT application - using a loop and decision and request the Python program. Your responses MUST include one flowchart and one Python program. Posed Questions are due by: Thursday @ 11:59 pm Responses are due by: Sunday @ 11:59 pm s3.us-east-1.amazonaws.com 16 Bb https://s3.us-east-1.amazonaws.com/blackboard.learn.xythos.prod/5a3o... SOLUTION: python question - Python Homework Help - Studypool Untitled + IT102 Chapter 5 Homework Homework Instructions: Timing: Submit your homework to Bb by 11:30pm of the day it is due in order to ensure that it has passed through Bb's gates and is ready to process before the end of the day. The last document you submit on time is the one Bb will grade. NO LATE HOMEWORK IS ACCEPTED FOR ANY REASON NOTE: If some documented medical situation interferes with your ability to get your work in on time, notify your instructor. Late homework is never graded; however, your instructor needs to know about your circumstances so that he/she can work with you to find the best options. Instructions for the layout of your Homework Document: You are asked to draw flowcharts, create Python program files, run programs, and obtain output in the IDLE window. Photos and/or screenshots of this work must be included in your HW document. Please ensure that everything is legible. The final document must be in PDF format. If you do not know how to create a PDF document, call or visit GMU'S ITS Support or a VSE lab with a student assistant. Show ALL of your work in order to get credit. There is a Power Point example of a HW submission on Bb under "Help with Course Materials" on the "Syllabus and Course Information site For these problems, 1. Write a Python program to: ask the user to enter two integers: inti and int2. The program uses the exponential operator to calculate and then print the result when int1 is raised the int2 power. You also want to calculate the result when int1 is raised to the 5 power; however, you realize that it is not possible to take the square root of a negative number. If the value for int that is entered is a negative number, print a message to the user explaining why you cannot complete the task. Otherwise, calculate the square root and print it. Finish the program by printing the values of int1 and int2. 2. For problem #1, when the user entered a negative number, you output a message that it was not possible to take the square root of a negative number. This time you will write a Python program that asks the user to enter a non-negative number (float). If the user does enter a negative number, you will use a while loop to continue prompting the user until you get a valid number i.e. a number >= 0). Once the user enters a valid number, you calculate and print the square root. Test your program twice, once using a negative number and once using a non-negative number. 1 s3.us-east-1.amazonaws.com 16 Bb M https://s3.us-east-1.amazonaws.com/blackboard.learn.xythos.prod/5a30... SOLUTION: python question - Python Homework Help - Studypool Untitled + IT102 Chapter 5 Homework 3. Given this flowchart, write the program code to implement the flowchart. The algorithm: the user is asked to input the customer's name and their debt1 amount and their debt2 amount. The total debt is calculated. If the total debt is greater than $900,000 the name and a message warning of danger is output. If not, but the total debt is greater than $450,000, the name and a message to reduce debt is output. Otherwise, the name and a message congratulating the customer on being careful is output. Start 1-0 input name input debt1 input debt2 total = debt1 + debt2 total >900,000 output name, a danger message N total >450,000 Output name, a y reduce debt message N output name, a congrats message Stop 2
Purchase answer to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

Three files are attache...


Anonymous
Great study resource, helped me a lot.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Similar Content

Related Tags