Skip to main content

Section 4.13 Write Code Questions

Activity 4.13.1.

Fix the errors in the code, and change it to use only one if statement. The code should print β€œThe number is 5” when the number is 5, and should print β€œThe number is NOT 5” when it is not.
Solution.
Fix the errors in the code, and change it to use only one if statement. The code should print β€œThe number is 5” when the number is 5, and should print β€œThe number is NOT 5” when it is not.

Activity 4.13.2.

Complete the conditional below so the word β€œHi” is printed if x does not equal 3 and β€œHello” is printed otherwise.

Activity 4.13.3.

Complete the pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours, if the regular pay rate is $10 an hour. Then set grossPay equal to the amount an employee would be paid for working 45 hours.
Solution.
Complete the pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours, if the regular pay rate is $10 an hour. Then set grossPay equal to the amount an employee would be paid for working 45 hours.

Activity 4.13.4.

Rewrite your pay program using try and except so that your program handles non-numeric input gracefully by printing a message and exiting the program. The following shows two executions of the program:
Enter Hours: 20
Enter Rate: nine
Error, please enter numeric input
Enter Hours: forty
Error, please enter numeric input

Activity 4.13.5.

Write the code to calculate and print the cost of a 14 mile cab ride. If the distance traveled is less than or equal to 12 miles the cost is $2.00 a mile, and if the distance traveled is more than 12 miles the cost is $1.50 a mile. Assign the final cost to the variable total.
Solution.
Write the code to calculate and print the cost of a 14 mile cab ride. If the distance traveled is less than or equal to 12 miles the cost is $2.00 a mile, and if the distance traveled is more than 12 miles the cost is $1.50 a mile. Assign the final cost to the variable total.

Activity 4.13.6.

Write a program to prompt for a score between 0.0 and 1.0. If the score is out of range, print an error message. If the score is between 0.0 and 1.0, print a grade using the following table:
 Score   Grade
>= 0.9     A
>= 0.8     B
>= 0.7     C
>= 0.6     D
 < 0.6     F
Enter score: 0.95
A

Activity 4.13.7.

Fix the example such that the cost of frozen yogurt is 0 if you pour exactly 1 lb. in your cup.
Solution.
Fix the example such that the cost of frozen yogurt is 0 if you pour exactly 1 lb. in your cup.

Activity 4.13.8.

Write a procedure that takes 2 ints, total price, and amount in wallet. Print β€œYou have enough money” if the difference between the wallet and price is 0 or greater; otherwise, print β€œGet more money”.

Activity 4.13.9.

3 criteria must be taken into account to identify leap years:
Write a program that takes a year as a parameter and sets leapYear equal to True if the year is a leap year, False otherwise. (use a few different years to test your work)
Solution.
3 criteria must be taken into account to identify leap years:

Activity 4.13.10.

Finish the following code. It first sets n to a number input by a user. Convert the number from a string to an integer and set result to True if the number is an even number (evenly divisible by two) and False if it is odd. Note: use the modulo operator.
You have attempted of activities on this page.