Presentation is loading. Please wait.

Presentation is loading. Please wait.

REVIEW No curveballs this time …. PROBLEM TYPE #1: EVALUATIONS.

Similar presentations


Presentation on theme: "REVIEW No curveballs this time …. PROBLEM TYPE #1: EVALUATIONS."— Presentation transcript:

1 REVIEW No curveballs this time …

2 PROBLEM TYPE #1: EVALUATIONS

3 Evaluate ExpressionOutputData Type 5 – 2 5 ** 2 “5” + “2” “5” * 2 5 / 2 5 // 2 5 % 2 5 + 2.0 5.0 * 2.0

4 Evaluate ExpressionOutputData Type 5 – 23integer 5 ** 225integer “5” + “2”52string “5” * 255string 5 / 22.5float 5 // 22integer 5 % 21integer 5 + 2.07.0float 5.0 * 2.010.0float

5 Evaluate ExpressionOutputData Type 10 > 2 2 < 1 2 != 4 4 == 2 ** 2 ‘cat’ < ‘dog’ ‘cat’ < ‘Dog’ 5 > 2 and 10 > 11 5 > 2 or 10 > 11 5 > 2 and not (10 > 11)

6 Evaluate ExpressionOutputData Type 10 > 2TrueBoolean 2 < 1FalseBoolean 2 != 4TrueBoolean 4 == 2 ** 2TrueBoolean ‘cat’ < ‘dog’TrueBoolean ‘cat’ < ‘Dog’FalseBoolean 5 > 2 and 10 > 11FalseBoolean 5 > 2 or 10 > 11TrueBoolean 5 > 2 and not (10 > 11)TrueBoolean

7 Evaluate ExpressionOutputData Type str.lower(“HeLLo”) str.upper(‘HeLLo’) format(5.1, ‘.2f’) format(5.1, ‘10.2f’) format(5, ‘10d’) random.randint(1,5) len(‘cat’) len(‘cat’ + ‘dog’) not (5 > 2 and 5 < 4 )

8 Evaluate ExpressionOutputData Type str.lower(“HeLLo”)hellostring str.upper(‘HeLLo’)HELLOstring format(5.1, ‘.2f’)5.10string format(5.1, ‘10.2f’) 5.10string format(5, ‘10d’) 5string random.randint(1,5){1, 2, 3, 4, 5}integer len(‘cat’)3integer len(‘cat’ + ‘dog’)6integer not (5 > 2 and 5 < 4 )TrueBoolean

9 PROBLEM TYPE #2: SHORT ANSWER

10 Short Answer – Sample Question ◦ Explain what a Boolean expression is and why you would use one while programming. Give an example in a short code.

11 Short Answer – Sample Question ◦ Explain what a Boolean expression is and why you would use one while programming. Give an example in a short code. Boolean expressions are conditional statements that compare two or more different values, using an operator (and, or, not, >, =, <=, ==) These expressions always hold a value of either True or False They are normally seen as the conditions of an “IF” statement

12 Short Answer – Sample Question ◦ Identify two different functions that convert one data type to another. Then use each in an example code and explain when you would use it in an actual program.

13 Short Answer – Sample Question ◦ Identify two different functions that convert one data type to another. Then use each in an example code and explain when you would use it in an actual program. str(argument) – converts any data type into a string literal int(argument) – converts an argument into an integer, but it must be given that it CAN be converted into an integer, (strings will cauase an error), for floats, it will always round the number down float(argument) – converts an argument into a float, given that the argument CAN be converted into a float

14 Short Answer – Sample Question ◦ Explain how the “and” and “or” operators work. Write a short code to show an example of each and describe how you would use it in an actual program.

15 Short Answer – Sample Question ◦ Explain how the “and” and “or” operators work. Write a short code to show an example of each and describe how you would use it in an actual program. The “and” operator checks for two conditions simultaneously and both conditions must hold True in order for the Boolean expression to hold True. The “or” operator checks for two conditions as well, but only one of them needs to hold True in order for the expression to hold True. It can be though of as checking one of two conditions.

16 Short Answer – Sample Question ◦ What’s the difference between using an “ELIF” statement and a nested decision structure? Describe both individually and explain the difference between them.

17 Short Answer – Sample Question ◦ What’s the difference between using an “ELIF” statement and a nested decision structure? Describe both individually and explain the difference between them. An ELIF statement is in essence the same thing as a nested decision structure. All ELIF statements can be written using a nested structure, ELIF’s are purely for convenience. However, they are very convenient because an ELIF statement extends a single decision structure while, any time an IF statement is written, you create a new decision structure, whether nested or not.

18 PROBLEM TYPE #3: TRACE THE OUTPUT

19 Trace the Output #1

20

21 Trace the Output #2

22

23 Trace the Output #3

24

25 Trace the Output #4 …(1of 3) This problem was made by a fellow student. I figured I would include it here because it serves as good practice. This is only the beginning … see next slide for continuation.

26 Trace the Output #4 …(2 of 3)

27 Trace the Output #4 …(3 of 3)

28 PROBLEM TYPE #4: DEBUGGING

29 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! ’ )

30 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

31 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) >> 3.4900000000002

32 Types of Errors ◦ Please pay close attention this time around to naming the type of error that is being made ◦ Realize the most common error is a syntax error, at least when looking at the source code Example: x = 3 print(x + “4”) # this is considered a syntax error because you # are trying to add an integer to a string

33 PROBLEM TYPE #5: WRITE A PROGRAM

34 Write a program that … ◦ Asks the user for two random words. ◦ Sort the words in alphabetical order and print them out. ◦ Sort the words in size order and print them out.

35 Write a program that …

36 ◦ Has the computer virtually roll two 6-sided dice. ◦ Output the result as follows: Virtual Dice Roll: 3 and 5

37 Write a program that …

38

39

40 ◦ Allows the user to qualify for a credit card. Requirements: -They must make at least $30,000 a year (disregard tax/deductions) -Their rent payment cannot be more than 50% of their total monthly salary -However, if they own their house, you do not need to take their monthly rent into account

41 Credit Card Qualifier: Sample Runs Credit Card Qualifier Annual Income: 40000 Do you own your house? (y/n): y You qualify! Credit Card Qualifier Annual Income: 40000 Do you own your house? (y/n): n Monthly Rent: 1200 You qualify! Credit Card Qualifier Annual Income: 50000 Do you own your house? (y/n): n Monthly Rent: 4000 Sorry, you do not qualify!

42 Write a program that …

43 ◦ Allows a user to input the price of 3 items they are purchasing in a store ◦ Then, calculate and print out the subtotal ◦ Calculate the tax (assume 7%) ◦ Finally, print out the total bill ◦ You should format your numbers for money-talk

44 Write a program that …

45 ◦ Allows a user to type in a 3 digit integer (assume, that the last digit will not be zero) ◦ Print out the original number ◦ Then, print out the number in reversed order Please enter a three digit number: 347 You entered: 347 347 backwards is 743

46 Write a program that …

47 ◦ Asks the user to type in a 2 digit integer (assume, that the last digit will not be zero), and call it variable x ◦ Then, figure out variable y, which is number with the same digits as x but reversed order ◦ Then, print out x and y and their sum Please enter a two digit number: 25 25 reversed is 52 25 + 52 is 77

48 Write a program that …

49 BEST OF LUCK!


Download ppt "REVIEW No curveballs this time …. PROBLEM TYPE #1: EVALUATIONS."

Similar presentations


Ads by Google