1. What is the difference between an if statement and an if-else statement? Be sure to state what the difference in meaning is between the two, not just the addition of the else keyword.

2. What type of value is returned by the relational operators?

3. What does it mean to Step Over code? What is that referring to?

4. What is a nested if statement?

5. How can nested if statements be avoided?

6. What is the general pattern for Guess and Check?

7. What is the Mathematician George Boole famous for?

8. When is it difficult to determine whether and or or should be used in an if

statement?

9. What is short circuit logic? When does it apply? Give an example of when it would apply. Do not use the example in the book.

10. What is the problem with comparing floats for equality?

11. If an exception occurs on line 2 of while executing this code give the line numbers of this program in the order that they are executed. What is the output from the program?

1 try :

2 x = int ( in p ut (" Please enter an integer :" ))

3 y = int ( in p ut (" Please enter another integer :" ))

4 exce p t :

5 p rint (" You entered an invalid integer .")

6 p rint (" The product of the two integers is " , x*y)

2.9 Exercises

1. Type in the code of Example 2.6. Execute the code using a debugger like the one included with the Wing IDE 101. Step into and over the code using the debugger. Enter a menu choice of 1. Using the line numbers in Example 2.6, which lines of the program are executed when you enter a 1 for the menu choice. List these lines. Do the same for each of the other menu choice values. If you run the program and enter a menu choice of 5, which lines of the program are executed. If you use the debugger to answer this question you will be guaranteed to get it right and you'll learn a little about using a debugger.

2. Write a program that prints a user's grade given a percent of points achieved in the class. The program should prompt the user to enter his/her percent of points. It should then print a letter grade A, A−, B+, B, B−, C+, C, C−, D+, D, D−, F. The grading scale is given in Fig. 2.11. Use exception handling to check the input from the user to be sure it is valid. Running the program should look like this:

Please enter your percentage achieved in the class : 92.32 You earned an A in the class .

3. Write a program that converts centimeters to yards, feet, and inches. There are

2.54 cm in an inch. You can solve this problem by doing division, multiplication, addition, and subtraction. Converting a float to an int at the appropriate time will help in solving this problem. When you run the program it should look exactly like this (except possibly for decimal places in the inches):

How many centimeters do you want to convert ? 127.25 This is 1 yard , 1 foot , 2.098425 inches .

Grade

If Greater Than Or Equal To

A

93.33

A-

90

B+

86.67

B

83.33

B-

80

C+

76.67

C

73.33

C-

70

D+

66.67

D

63.33

D-

60

F

0

Fig. 2.11 Grading scale

This is a modification of the program in Exercise 5 of Chap. 1. In this version of it you should print “yard” when there is one yard, and “yards” when there is more than one yard. If there are zero yards then it should not print “yard” or “yards”. The same thing applies to “feet”. Use an if statement to determine the label to print and if the label should be printed at all.

4. Write a program that computes the minimum number of bills and coins needed to make change for a person. For instance, if you need to give $34.36 in change you would need one twenty, one ten, four ones, a quarter, a dime, and a penny. You don't have to compute change for bills greater than $20 dollar bills or for fifty cent pieces. You can solve this problem by doing division, multiplication, subtraction, and converting floats to ints when appropriate. So, when you run the program it should look exactly like this:

How much did the item cost : 65.64

How much did the person give you : 100.00 The person 's change is $ 34 . 36

The bills or the change should be :

1 twenty

1 ten

4 ones

1 quarter

1 dime

1 penny

This is a modification of the program in Exercise 6 of Chap. 1. In this version, only non-zero amounts of bills and change should be printed. In addition, when only one bill or coin is needed for a particular denomination, you should use the singular version of the word. When more than one bill or coin for a denomination is needed, the plural of the label should be used.

5. Write a program that asks the user to enter an integer less than 50 and then prints whether or not that integer is prime. To determine if a number less than 50 is prime you only need to divide by all prime numbers that are less than or equal to the square root of 50. If any of them evenly divide the number then it is not prime. Use the guess and check pattern to solve this problem. Use exception handling to check the input from the user to be sure it is valid. A run of the program should look like this:

Please enter an integer less than 50 : 47

47 is prime .

6. Write a program that converts a decimal number to its binary equivalent. The decimal number should be read from the user and converted to an int. Then you should follow the algorithm presented in Example 1.1 to convert the decimal number to its binary equivalent. The binary equivalent must be a string to get the correct output. In this version of the program you must handle all 16-bit signed

integers. That means that you must handle numbers from −32768 to 32767. In this version of the program you should not print any leading 0's. Leading 0's

should be omitted from the output.

If you want to check your work, you can use the bin function. The bin function will take a decimal number and return a string representation of that binary number. However, you should not use the bin function in your solution.

The output from the program must be identical to this:

Please enter a number : 83

The binary equivalent of 83 is 1010011.

7. Write a program that prompts the user to enter a 16-bit binary number (a string of 1's and 0's). Then, the program should print the decimal equivalent. Be sure to handle both negative and positive binary numbers correctly. If the user enters less than 16 digits you should assume that the digits to the left of the last digit are zeroes. When run the output should look like this:

Please enter a 16 bit binary number : 1010011

The base 10 equivalent of the binary number 1010011 is 83 .

To handle negative numbers correctly you first need to detect if it is a negative number. A 16-digit binary number is negative if it is 16 digits long and the leftmost digit is a 1. To convert a negative number to its integer equivalent, first take the 1's complement of the number. Then convert the 1's complement to an integer, then add 1 to the integer and negate the result to get the 2's complement.

The conversion from bits to an integer can be carried out by multiplying each bit by the power of 2 that it represents as described in Sect. 1.5 of Chap. 1.

8. Converting numbers to any base can be accomplished using the algorithm from Example 1.1. For instance, an integer can be converted to hexadecimal using this algorithm. Hexadecimal numbers are base 16. That means there are 16 possible values for one digit. Counting in hexadecimal starts 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f, 10, 11, 12 and so on. The algorithm changes so that instead of dividing by 2 you divide by 16. The one gotcha is that if the remainder after dividing is greater or equal to 10 (base 10) then you should not append the base 10 value to the string. Instead you should append a, b, c, d, e, or f. You can use if statements to determine the correct value to append. Write a program that prompts the user to enter an integer and then prints its hexadecimal equivalent. Traditionally, hexadecimal numbers start with a “0x” to identify them as hex, so your output should look like this:

Please enter an integer : 255

The hexadecimal equivalent is 0 x00ff

Your program should handle any base 10 integer from 0 to 65535. There is a function called hex in Python that converts integers to their hexadecimal representation. You may not use this in implementing this program, but you may use it to see if your program is producing the correct output. For instance, calling hex(255) will return the string 0xff.

You should check the input that the user enters to make sure that it is in the valid range accepted by your program.

Found a mistake? Please highlight the word and press Shift + Enter