How can I get only 1 result for printing to the console, and not both of them?

My code has a problem, but I know what the problem is. There are 2 characters in the class, so 2 results are shown, each result adds 0.3. This is a problem because only one result should be printed on the console.

keydict = {'A': 4.0,'B': 3.0,'C': 2.0,'D': 1.0,'F': 0, '+': 0.3, '-': -0.3}
grade = input('Grade:')


def letter2number(letter):
    if len(grade) > 2:
        print('Too many characters')
        letter2number(grade)
    for char in grade:
        if char in keydict:
            if '+' in grade:
                print(keydict[char] + keydict['+'])
            elif '-' in grade:
                print(keydict[char] + keydict['-'])
            else:
                print(keydict[char])
        else:
            print(grade,'is an invalid input')
    return


letter2number(grade)

This is what is printed on the screen after the user enters "A +" (or any sort with "+" or "-"). I understand that the for loop causes this problem because there are 2 characters in the input. But I don't think there is another way to do this without using a for loop. A value of 4.3 is expected to be 0.6. This is not what I want.

Grade: A+ # <<< Thats a user input
4.3       # <<< YES
0.6       # <<< How can I get this to NOT print

Besides printing 0.6, I have no other code problems. In other words, I don't want 0.6, but 4.3 should stay.

+4
4

for . , for

"A"  # evaluates to 4.0, add 0.3 since '+' exists in the string
"+"  # evaluates to 0.3, add 0.3 since '+' exists in the string

, . :

def lettertograde(letter):
    letter, *modifier = letter
    grade = keydict[letter]
    if "+" in modifier:
        grade += 0.3
    elif "-" in modifier:
        grade -= 0.3
    return grade

a, *b = ... splat , :

a, *b = "A"          # a = "A", b = []
a, *b = "A+"         # a = "A", b = ["+"]
a, *b = "A-"         # a = "A", b = ["-"]
a, *b = "Good job!"  # a = "G", b = ["o", "o", "d", " ", "j", "o", "b", "!"]
a, *b = ""           # ValueError("not enough values to unpack (expected at least 1, got 0)")

N.B. .

a, *b, c, *d = "anything"  # SyntaxError("two starred expressions in assignment")
+5
>>> keydict = {'A': 4.0,'B': 3.0,'C': 2.0,'D': 1.0,'F': 0, '+': 0.3, '-':    0.3, ' ': 0.0}
>>> grade = input('Grade:') + ' '
>>> if grade[0] in keydict and grade[1] in keydict:
       print(grade, keydict[grade[0]]+ keydict[grade[1]])
    else:
       print(grade, 'is an invalid input')

B 3.0 - B- 2,7

+1

You need to add breakat the end of the blockif char in keydict:

keydict = {'A': 4.0,'B': 3.0,'C': 2.0,'D': 1.0,'F': 0, '+': 0.3, '-': -0.3}
grade = input('Grade:')


def letter2number(letter):
    if len(grade) > 2:
        print('Too many characters')
        letter2number(grade)
    for char in grade:
        if char in keydict:
            if '+' in grade:
                print(keydict[char] + keydict['+'])
            elif '-' in grade:
                print(keydict[char] + keydict['-'])
            else:
                print(keydict[char])

            break  # <<---

        else:
            print(grade,'is an invalid input')
    return


letter2number(grade)

break stops the cycle

0
source

Create modified grades and add them to the dictionary. Then find the entire line of the class.

>>> keydict = {'A': 4.0, 'B': 3.0, 'C': 2.0, 'D': 1.0, 'F': 0}
>>> modifiers = {'+': 0.3, '-': -0.3}
>>> keydict.update({ k + mk: v + mv for k, v in keydict.items() for mk, mv in modifiers.items() })
>>> keydict
{'D': 1.0, 'F+': 0.3, 'A+': 4.3, 'F': 0, 'F-': -0.3, 'C+': 2.3, 'B': 3.0, 'C': 2.0, 'D-': 0.7, 'C-': 1.7, 'B-': 2.7, 'D+': 1.3, 'B+': 3.3, 'A-': 3.7, 'A': 4.0}
>>> grade = 'C-'
>>> keydict[grade]
1.7
0
source

Source: https://habr.com/ru/post/1690410/


All Articles