Posts: 2
Threads: 1
Joined: Jul 2018
Jul-24-2018, 01:14 AM
(This post was last modified: Jul-24-2018, 01:28 AM by Segovia.)
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" "
Posts: 12,046
Threads: 487
Joined: Sep 2016
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.
Posts: 1,950
Threads: 8
Joined: Jun 2018
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.
Posts: 8,168
Threads: 160
Joined: Sep 2016
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
Posts: 1,950
Threads: 8
Joined: Jun 2018
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
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.
Posts: 8,168
Threads: 160
Joined: Sep 2016
(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 
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
Posts: 1,950
Threads: 8
Joined: Jun 2018
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.
Posts: 2
Threads: 1
Joined: Jul 2018
(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.
|