[Tutor] REport Card Question

Robert Sjöblom robert.sjoblom at gmail.com
Tue Nov 30 03:28:19 CET 2010


>>  Write a code that will take an input from a user (numerical grade) and
>> convert their numerical grade into a letter grade that is accompanied by a
>> ?smart? statement.
>>
>>  def grade_score(grade):
>>
>>     if grade >=95 and grade <= 100:
>>
>>         print 'A+, Excellent'
>>
>>     elif grade >=85 and grade < 95:
>>
>>         print 'A, Good Work'
>>
>>     elif grade >=80 and grade < 85:
>>
>>         print 'A-, Good Work, but you could do better'
[snip]
>> grade = raw_input('Put your grade here:?)
>>
>>
>> grade_score()
>>
>>
>> Put your grade here:77
>>
>> Traceback (most recent call last):
>>
>>   File "/Users/andre.jeyarajan/Documents/workspace/Chapter 5
>> Problems/src/ReportCardQuestion.py", line 28, in <module>
>>
>>     grade_score()
>>
>>
> This line tells you why it doesn't work:
>
>
>> TypeError: grade_score() takes exactly 1 argument (0 given)
>>
>
> Are you familiar with functions? When you define the function you define it
> as taking one parameter:
>
> def grade_score(grade):
>    # Code here
>
> but then when you call it:
>
> grade_score()
>
> you don't give it any parameters (or arguments). The traceback tells you
> that you need to provide it 1 argument and that you gave it 0 instead.
>
> If you called
>
> grade_score(3, 150)
>
> you would get a similar error, only it would say (2 given) instead.
>
> HTH,
> Wayne

On a related note, do all functions implicitly contain "return None"
in them? Trying out this function (correctly) would get "None" added,
such as:
Enter grade:76
B, Try Harder
None

Is there a way to avoid "return None" without explicitly having the
function return something? Even an empty string will return a new
line.

best regards,
Robert S.


More information about the Tutor mailing list