-1
a = 4.0
b = 3.0
c = 2.0
d = 1.0
f = 0.0
counter = 0
gpa = 0
while True:
    grade = input("what is your grade ")
    if(grade == "A" or "a"):
        counter += 1
        gpa += a
    elif(grade == "B" or "b"):
        counter += 1
        gpa += b
    elif(grade == "C" or "c"):
        counter += 1
        gpa += c
    elif(grade == "D" or "d"):
        counter += 1
        gpa += d
    elif(grade == "F" or "f"):
        counter += 1
        gpa += f
    elif (grade == ""):
        finalgrade = (gpa/counter)
        print(finalgrade)
        break
    else:
        print ("invalid input")

So I have been trying to get this work but no matter what I do i cant figure it out..... when I use the debugger it doesn't help much but it says that goes right past all of the elif statements

2 Answers2

1

This:

if grade == "A" or "a"

Doesn't really do what you think it does. Due to operator precedence in python, it computes grade=="A", and then or with to "a". This, will always evaluates to True.

Use the in operator instead:

if grade in ("A", "a"):

Or, even better:

if grade.lower() == "a":
aIKid
  • 26,968
  • 4
  • 39
  • 65
0

Instead of

grade == "A" or "a" #always True, since "a" is truthy

use

grade == "A" or grade == "a"

or

grade.upper() == "A"

Change the other statements similarly.

Though I would do

 import itertools

 points = {'A':4, 'B':3, 'C':2, 'D':1, 'F':0}

 total = 0.
 for count in itertools.count():
     grade = input('What is your grade ')
     if not grade:
         break
     try:
         total += points[grade]
     except KeyError:
         print('invalid input')
 print(total / count)
Paul Draper
  • 78,542
  • 46
  • 206
  • 285