Lab 6: Chained Conditional and Logical Operator Practice¶

In this lab, you'll practice writing if-elif-else statements as well as using some logical operators.

The algorithm for determining whether a year is a leap-year is more complex than many people realize. According to https://en.wikipedia.org/wiki/Leap_year , the algorithm written in pseudocode (an informal code-like notation for communicating algorithms for other humans to read) is

if (year is not divisible by 4) then (it is a common year)

else if (year is not divisible by 100) then (it is a leap year)

else if (year is not divisible by 400) then (it is a common year)

else (it is a leap year)

Exercise 1: Implement this using Python syntax and the if-elif-else pattern.

Exercise 2: Due to state regulations, a toy company is not allowed to sell silly string in California and Texas. Their ordering software looks like this:

product = input("What do you want to buy? ")
state = input("What state do you live in? ")

if state == "California" or state == "Texas" and product == "silly string":
    print("Sorry, we can't sell too you.")
else:
    print("You're good to go.")
What do you want to buy? Whoopee Cushion
What state do you live in? California
Sorry, we can't sell too you.

However, there seems to be a problem - it doesn't let Californians buy Whoopee Cushions, but there shouldn't be any restrictions on Whoopee Cushions.

  • What is the problem with this code?
  • How do you fix it?

Exercise 3: A loan applicant should be approved if their salary is $40,000/year or more and if they have been at the same job for at least 2 years. Write a program that will ask the user for their salary and number of years on the job, and then tell them whether they got the loan.

Challenge Exercise 4: Write a program that will ask the user for their grade as a percentage and then display the associated letter grade according to this table:

Percentage Letter Grade
90-100 A
80-89 B
70-79 C
60-69 D
0 - 59 F

If the user enters a number greater than 100 or less than 0, report that it is invalid input. Write your output so that it looks like this:

When you are finished, submit your solution to the Lab 6: Letter grade assignment on codePost. It will be autograded, and it'll be looking for the output to match the examples above.