Date

Lecture Date: Monday, September 19

More on if and else today.

Let's build a grade calculator!

Consider a few different ways of figuring out the letter grade:

  • several if statements
  • if-elif statement
  • by looking at the individual digits of the grade

Here's something to consider:

# Prompt the user

grade = int(input("What's your grade? "))

tens = grade // 10
ones = grade % 10

For example, we can intentionally leverage integer division to get the tens place of the grade. To get the ones place, we can use the modulus operator, which gives us the remainder after division.

Let's build a simple program together. Write a program called letter_grade.py that will take a list of grades, separated by spaces, and then calculate the average of those grades. Print to the screen the average along with the letter grade (according to our letter grade system found in the syllabus). The scores are not weighted in any way - just do a straight up average.

So, a run of the program might look like this:

Input your scores, separated by spaces: 90 80 78
Average: 82.667
Letter: B-

We will spend time today touching on the following:

  • Scope (and scope errors)
  • Reviews the logical operators: == != < > <= >= and or not
  • tracing code

Then, we'll start on loops if we have time, specifically while loops.

index = 1
while index < 10:
    print(index)
    index = index + 1 # or index += 1

# infinite = 1
# while infinite > -1:
#     print(infinite)
#     infinite += 1

value = 5
while value >= 0 and value <= 10:
    value = int(input("Give me a number less than 0 or greater than 10:"))

value2 = -1
while value2 <= -1 or value2 > 10: # this is the bad case
    value2 = int(input("Give me a number between 0 and 10: "))

list_of_names = ["mark" , "tom"]
while len(list_of_names) > 0:
    # do something