Presentation is loading. Please wait.

Presentation is loading. Please wait.

Few More Math Operators

Similar presentations


Presentation on theme: "Few More Math Operators"— Presentation transcript:

1 Few More Math Operators
Just a couple of more …

2 Practice – The solution to all our problems
We’ve all been there before, we go to the restaurant with some friends and we’re having a good time … all until the check comes out and the nightmare that is figuring out how much you need to pay appears.

3 Practice – The solution to all our problems
Create a program that helps a user figure out exactly how much they need to pay for dinner. You probably want to start off with a design for your code. (What information do you need to start? What parts of the solution are subject to change?) Assume 7% tax.

4 Practice – The solution to all our problems
HINT: You will need to ask the user for a few components: The total amount of the check (before tax) The price of their individual meal The percent tip they want to leave The number of people at the table

5 Practice – The solution to all our problems
Step by step: You will need to take the total amount of the check and add the tax to the total Then you want to find the tip based off the total before tax Then you will add the tax amount and the tip amount as a table You will take that number and divide it by the number of people who ate, this will be the amount people need to put on top of their meal for “tip and tax” Add this value to the amount of the user’s specific meal

6 The REAL solution … Understand that the actual problem at hand was not the calculations of the bill People are more often frustrated by the time they feel they’re wasting by waiting for the check to be paid … So, what’s the real solution?

7 The REAL solution …

8 It’s your birthday! Write a program that asks the user for their name and their age. Figure out what their birth year is from this information and report it back to the user.

9 Debugging The process of debugging is really to find errors (bugs) and then to resolve them

10 Types of Errors Syntax errors: the code does not follow the rules for the language; for example, a single quote is used where a double quote is needed, or a colon is missing, or a keyword is used as a variable name print ( “ Hello, world! ’ )

11 Types of Errors Runtime errors: in this case, your code is fine but the program does not run as expected (it “crashes”). For example, if your program is meant to divide two numbers, but the divisor is zero, a runtime error will occur. var1 = float( input (‘give me a number:’) ) number = var1 + 4 >> give me a number: seventeen thirty eight # runtime error

12 Types of Errors Logic errors: these can be the hardest to find. In this case, the program is correct from a syntax perspective, and it runs, but the result is not what you were looking for. For example, if your program prints “2 + 2 = 5” the answer is … clearly wrong. var = 3.49 print (var) >>

13 Example Logic Error num_1 = float ( input ( ‘ give me a number:’ ) ) num_2 = float ( input ( ‘ give me another number:’ ) ) Print ( ‘ the sum is: ’, num_1 – num_2 ) >> give a number: 43 give me another number: 32 the sum is: 11

14 Tips for debugging Set small, incremental goals for your program. Don’t try and write a large program all at once. Stop and test your work often. Use comments to have Python ignore certain lines that are giving you trouble. Ask a friend.

15 Practice: Debugging # Name: Donald Seok # Title: “I don’t always make mistakes, but when I do …” first_name = ‘Donald” last_name = Seok print ( “Welcome to “Computer Programming” 101,”, first_name last_name, “!” ) print ( ) answer_1 = float ( input ( “What is the answer to 5 plus 2?” ) Print (“Your answer:”, answer_1) print ( )) real_answer = 5 * 2 printt (“If you answered”, real_answer, “you’re right!”)

16 Order of Operations Python follows the order of operations (PEMDAS)
You can use parentheses inside your math expressions to group operations Example: x = ( ( ) / 60 ) * 100

17 PEMDAS Multiplication/Division, Addition/Subtraction must be done in order that it shows up, not interchangeable 3 * 4 / 2 * 5 (3 * 4) / (2 * 5)

18 Converting Math Formulas into Programming Statements
Most math formulas need to be converted into a format that Python can understand Examples: 10 x y * x * y ( 3 ) ( 12 ) * 12 𝑦= 3𝑥 y = 3 * x / 2

19 Line Continuation Sometimes expressions can get to be very long
You can use the “ \ ” symbol to indicate to Python that you’d like to continue the expression onto another line ** Example: x = / \ + 8 – 12 This also works for the print ( ) function

20 Mixed Type Expressions
Python allows you to mix integers and floats when performing calculations The result of a mixed-type expression will evaluate based on the operands used in the expression Operand 1 Operand 2 Result int float

21 Division Operations Python contains two different division operators
The “/” operator is used to calculate the floating-point result of a division operation The “//” operator is used to calculate the integer result of a division operation, it will throw away the remainder. *** This operation will always round DOWN. Examples: print ( 5 // 2 ) # 2 print ( -5 // 2 ) # -3

22 Remainder Operator (modulo)
The modulo operator “ % ” returns the remainder portion of a division operation This is basically the opposite of the “ // ” operator Examples: 5 / # 2.5 5 // 2 # 2 5 % 2 # 1 (remainder from divisor of 2)

23 Exponents You can raise any number to a power by using the “ ** ” operator Example:  ** 4

24 Practice: Time Calculations
Ask the user to input a number of seconds as a whole number. Then express the time value inputted as combination of minutes and seconds >> Enter seconds: 110 That’s 1 minute and 50 seconds!

25 Practice: Time Calculations
Now extend this program to include the number of hours >> Enter seconds: 12074 That’s 3 hours, 21 minutes and 14 seconds!

26 Escape Key “\” The backslash ( “ \ ” ) is known as an escape key in Python It tells Python that the character directly following the backslash will not function in it’s regular nature Example: print (“This class is “Awesome!””) #error! print (“This class is \“Awesome!\””) >> This class is “Awesome!”

27 Examples of the Escape Key
We can use the escape key in various ways: print(“\””) # this will print a quotation mark print(“\n”) # this will print a new line print(“\t”) # this will print an indented tab print(“\\”) # this will print out a back slash

28 Examples of the Escape Key
print (“We saw this \n this will print a new line”) >> We saw this this will print a new line

29 Examples of the Escape Key
print (“We saw this \t this will print a tab”) >> We saw this this will print a tab

30 Examples of the Escape Key
print (“What if we want an actual backslash \\”) >> What if we want an actual back slash \

31 Practice: O Christmas Tree
Using a single print statement, try writing a program that prints out the image of a Christmas Tree We want this: >> tree /\ / \ / \ I I

32 Examples of the Escape Key
print (""" /\\ \n / \\ \n/ \\ \n | | """) >> tree /\ / \ / \ I I


Download ppt "Few More Math Operators"

Similar presentations


Ads by Google