Python Forum
Homework Help - Simple Grading System
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Homework Help - Simple Grading System
#1
The assignment is to create a grading system. Not completely sure what I'm doing wrong at this point. Any suggestion or insight to fix my syntax error?

def convertgrade(name, grade):
score = grade
grade = float(grade)
grade = int(grade)
if 100 >= grade >=94: return 'A'
if 94 > grade >=90: return 'A-'
if 90 > grade >=87: return 'B+'
if 87 > grade >=84: return 'B'
if 84 > grade >=80: return 'B-'
if 80 > grade >=78: return 'C+'
if 78 > grade >=75: return 'C'
if 75 > grade >=70: return 'C-'
if 70 > grade >=65: return 'D+'
if 65 > grade >=60: return 'D'
if 60 > grade: return 'E'

var = 1
while var == 1:
name = raw_input("What is the students name?")
score = raw_input("what is the students original grade?")
print"Converted Grade:", convertedgrade(name, score)
print" "

Syntax Error: invalid syntax: line 44, pos 27
print"Converted Grade:", convertedgrade(name, score)

def convertgrade(name, grade):
score = grade
grade = float(grade)
grade = int(grade)

if 100 >= grade >=94: return 'A'
if 94 > grade >=90: return 'A-'
if 90 > grade >=87: return 'B+'
if 87 > grade >=84: return 'B'
if 84 > grade >=80: return 'B-'
if 80 > grade >=78: return 'C+'
if 78 > grade >=75: return 'C'
if 75 > grade >=70: return 'C-'
if 70 > grade >=65: return 'D+'
if 65 > grade >=60: return 'D'
if 60 > grade: return 'E'

var = 1
while var == 1:
    name = raw_input("What is the students name?")
    score = raw_input("what is the students original grade?")
    print"Converted Grade:", convertedgrade(name, score)
    print" "
Reply
#2
lines 2-16 must be indented 4 spaces to be part of the function
this will get you started

There are formatting issues, but this will come with experience,
for example the return statements should be on a separate line, indented 4 spaces.
the if statement can be greatly simplified if you think about it a bit.
Reply
#3
Usually it goes this way: you should have idea what you want to accomplish and then you should think how to express it in Python.

Currently there are statements which don't fit together very well:

def convertgrade(name, grade):
    score = grade                # why rename grade?
    grade = float(grade)         # why convert grade?
    grade = int(grade)           # why re-convert grade, making step #2 pointless
There is no need to declare types in Python. You can try following in Python interactive interpreter:

>>> grade = 100
>>> type(grade)
<class 'int'>
>>> grade = 'one hundred'
>>> type(grade)
<class 'str'>
Without any explicit type conversion grade type changed from int to str, purely based on object you bind to name.

There are several possibilities how to do it differently. Simplest way is to get rid of if's and use simple for - loop:

def score_to_grade(score):
    breakpoints = [60, 65, 70, 75, 78, 80, 84, 87, 90, 94, 101]
    grades = ['E', 'D', 'D+', 'C-', 'C', 'C+', 'B-', 'B', 'B+', 'A-', 'A']
    for i, v in enumerate(breakpoints):
        if score < v:
            return grades[i]
Another option is to use Python module bisect for such kind of tasks. I understand that this is not what is expected from you, but just to give you idea how can it be done differently:

from bisect import bisect

def score_to_grade(score):
    breakpoints = [60, 65, 70, 75, 78, 80, 84, 87, 90, 94]
    grades = ['E', 'D', 'D+', 'C-', 'C', 'C+', 'B-', 'B', 'B+', 'A-', 'A']
    i = bisect(breakpoints, score)
    return grades[i]
In both cases you feed function with score and it returns grade.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
the first example snippet, but with zip, instead of enumerate:
def score_to_grade(score):
    breakpoints = [60, 65, 70, 75, 78, 80, 84, 87, 90, 94, 101]
    grades = ['E', 'D', 'D+', 'C-', 'C', 'C+', 'B-', 'B', 'B+', 'A-', 'A']
    for breakpoint, grade in zip(breakpoints, grades):
        if score < breakpoint:
            return grade
and thanks for mentioning the bisect module
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Just as note of interest.

Functions with enumerate and zip are not equivalent to the function using bisect. The difference is in how out of range values in upper end are handled.

Function using bisect always returns grade. If score is 1000, it will be 'A'. Functions using enumerate or zip will not return anything if score is larger than 100. Is it expected behaviour or not depends how your code is written. It is good practice to validate user input and feed function with validated value. By doing this it doesn't matter how function behaves out of range. However, we do not always behave Smile
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
(Jul-24-2018, 07:48 AM)perfringo Wrote: Just as note of interest.

Functions with enumerate and zip are not equivalent to the function using bisect. The difference is in how out of range values in upper end are handled.

Function using bisect always returns grade. If score is 1000, it will be 'A'. Functions using enumerate or zip will not return anything if score is larger than 100. Is it expected behaviour or not depends how your code is written. It is good practice to validate user input and feed function with validated value. By doing this it doesn't matter how function behaves out of range. However, we do not always behave Smile


good point. one can make them work the same with
def score_to_grade(score):
    breakpoints = [60, 65, 70, 75, 78, 80, 84, 87, 90, 94]
    grades = ['E', 'D', 'D+', 'C-', 'C', 'C+', 'B-', 'B', 'B+', 'A-']
    for breakpoint, grade in zip(breakpoints, grades):
        if score < breakpoint:
            return grade
    else:
        return 'A'
however if you want to validate the input (reject score >100)
def score_to_grade(score):
    breakpoints = [60, 65, 70, 75, 78, 80, 84, 87, 90, 94, 101]
    grades = ['E', 'D', 'D+', 'C-', 'C', 'C+', 'B-', 'B', 'B+', 'A-', 'A']
    for breakpoint, grade in zip(breakpoints, grades):
        if score < breakpoint:
            return grade
    else:
        raise ValueError('Score can  not be greater than 100')
problem with score < 0 remains however. one need to reject it before the for loop
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
One way to validate input is write a separate function, something like that:

def validate(request):
    """Return allowed input in range 1-100"""
    allowed = range(1, 101)
    m = (
        (f'Expected integer in range 1 - 100 '
         f'but input was')
        )
    
    while True:
        answer = input(request)
        try:
            answer = int(answer)
            if answer in allowed:
                return answer
        
        except ValueError:
            print(f'{m} {answer}')
            
        else:
            print(f'{m} {answer}')
Then it's just:

score_to_grade(validate('Enter score: '))
and now it is not important how score_to_grade handles out of range values.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#8
(Jul-24-2018, 02:40 AM)Larz60+ Wrote: lines 2-16 must be indented 4 spaces to be part of the function
this will get you started

There are formatting issues, but this will come with experience,
for example the return statements should be on a separate line, indented 4 spaces.
the if statement can be greatly simplified if you think about it a bit.

Thank you, Yes I'm just starting with python. So definitely have a substantial amount of learning to do and practice to be had.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Where can I get loooads of simple homework questions for kids? omar 1 975 Nov-09-2022, 02:33 PM
Last Post: Larz60+
  [TkInter]Simple Python Homework zaji_123 4 4,065 Jan-01-2021, 07:09 PM
Last Post: EliesBe
  Please help. Simple homework Asm0deus314 3 3,371 Feb-02-2020, 07:03 PM
Last Post: michael1789
  homework - banking system Massimo16 2 3,250 Jan-13-2020, 11:02 PM
Last Post: jefsummers
  homework - banking system Massimo16 1 4,690 Jan-12-2020, 03:56 PM
Last Post: ibreeden
  Grading tool Stuffed_crust 2 2,592 Nov-16-2018, 02:59 AM
Last Post: stullis
  i've got these very simple homework HakolYahol 7 5,480 Aug-12-2018, 10:18 AM
Last Post: HakolYahol

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020