4.13. Write Code Questions

  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.

    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.

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

  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.

    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.

  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
    
  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.

    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.

  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
    
  7. Fix the example such that the cost of frozen yogurt is 0 if you pour exactly 1 lb. in your cup.

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

  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”.

  9. 3 criteria must be taken into account to identify leap years:

    • The year is evenly divisible by 4;

    • If the year can be evenly divided by 100, it is NOT a leap year, unless;

    • The year is also evenly divisible by 400. Then it is a leap year.

    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)

    3 criteria must be taken into account to identify leap years:

    • The year is evenly divisible by 4;

    • If the year can be evenly divided by 100, it is NOT a leap year, unless;

    • The year is also evenly divisible by 400. Then it is a leap year.

    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)

  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