Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

printing - Python Script returns unintended "None" after execution of a function


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In python the default return value of a function is None.

>>> def func():pass
>>> print func()     #print or print() prints the return Value
None
>>> func()           #remove print and the returned value is not printed. 
>>>

So, just use:

letter_grade(score) #remove the print

Another alternative is to replace all prints with return:

def letter_grade(score):
    if 90 <= score <= 100:
        return "A"
    elif 80 <= score <= 89:
        return "B"
    elif 70 <= score <= 79:
        return  "C"
    elif 60 <= score <= 69:
        return "D"
    elif score < 60:
        return "F"
    else:
        #This is returned if all other conditions aren't satisfied
        return "Invalid Marks"

Now use print():

>>> print(letter_grade(91))
A
>>> print(letter_grade(45))
F
>>> print(letter_grade(75))
C
>>> print letter_grade(1000)
Invalid Marks

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...