Neeed some help! could anyone help me?Thanks! btw am a noob

So I was doing a problem on the textbook, and it says " using a function called computegrade that takes a score as its parameter and returns a grade as a string."

my question is, how come that it returns more than one string? Where did I go wrong?
PLZ help me, thanks everyone, I stuck this for 2 days and had no clue why.

def computegrade(score):
score=(0.5,0.6,0.7,0.8,0.9,1.0)

for score in [0.5,0.6,0.7,0.8,0.9,1.0]:
    if score>0.9:
        print('a')
    elif 0.8<=score<0.9:
        print ('b')
    elif 0.7<=score<0.8:
        print('c')
    elif 0.6<=score<0.7:
        print('d')
    elif score<0.6:
        print('failed')
    else:
        print('no answer')

nam=input(‘enter your score’)
print(computegrade(nam))


C:\Users\94217\AppData\Local\Programs\Python\Python38\python.exe C:/Users/94217/Desktop/CODING/Practice/functions.py
enter your score0.8
failed
d
c
b
no answer
a
None

Process finished with exit code 0

By Timjinlun via Discussions on Python.org at 07Jun2022 23:02:

So I was doing a problem on the textbook, and it says " using a
function called computegrade that takes a score as its parameter and
returns a grade as a string."

my question is, how come that it returns more than one string? Where did I go wrong?
PLZ help me, thanks everyone, I stuck this for 2 days and had no clue why.

Thank you for including your code and example output. BTW, paste code
between triple backticks to preserve the indentation:

```python
your python code here
```

and other output like this:

```
output here
```

That will keep the formatting.

Here’s your function:

def computegrade(score):
    score=(0.5,0.6,0.7,0.8,0.9,1.0)
    for score in [0.5,0.6,0.7,0.8,0.9,1.0]:
        if score>0.9:
            print('a')
        elif 0.8<=score<0.9:
            print ('b')
        elif 0.7<=score<0.8:
            print('c')
        elif 0.6<=score<0.7:
            print('d')
        elif score<0.6:
            print('failed')
        else:
            print('no answer')

The first thing it does is throw away the value of score which was
passed to the function, and replace it with a tuple of several values.
Then it ignores that as well and runs a for-loop against a list of the
same values. And the function does not return a grade, it prints a
grade. These are different things.

You want your function to accept a single score and return a single
grade. So it should loook like this:

def computegrade(score):
    if score>0.9:
        return 'a'
    elif 0.8<=score<0.9:
        return 'b'
    elif 0.7<=score<0.8:
        return 'c'
    elif 0.6<=score<0.7:
        return 'd'
    elif score<0.6:
        return 'failed'
    else:
        return 'no answer'

That does not print anything - printing is for the caller of the
function, which you do here:

nam=input('enter your score')
print(computegrade(nam))

If you wanted to test several scores you might use your for-loop, again
as a caller of the computegrade function:

for score in [0.5,0.6,0.7,0.8,0.9,1.0]:
    print(score, "=>", computegrade(score))

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like

Instructions from the textbook: return a grade.

Your code: print a letter score.

print and return are different.

They are sometimes easy to confuse because, in the interactive interpreter (with the >>> prompt) if a function returns a value and you do nothing with it, the interpreter will automatically print it.

To see more of the differences between print and return, try these two functions and see how they are different:


def return_example():
    return 1
    return 2
    return 3

def print_example():
    print(1)
    print(2)
    print(3)

value = return_example()
print("the return example gives", value)
print("** ** ** ** **")
value = print_example()
print("the print example gives", value)

Save that in a file and run it. Can see see the difference between
printing a value and returning a value?

2 Likes

The simple answer to your question is :
The for: loop is not processing the score that was entered and passed to the function. The for: loop is processing the list [0.5,0.6,0.7,0.8,0.9,1.0].

Also- the “no answer” output for the value 0.9 is because your code only checks for Greater Than 0.9 or Less Than 0.9. Your Print('a') condition (or return condition) needs to be

if score>=0.9:
1 Like

Thank you sooo much !!!That helps me a lot!!!

I finally got it! Thanks!!
Cheers!

Thank you!!! I finally got it!

Now I think I have a better understanding of the difference between ‘return’ and ‘print’ thanks to your examples.

When calling a ‘return’ of a function it only gives you back a value, but if you don’t ‘print’ it, the interpreter won’t give you any output. So ‘print’ is more like executing an order right?

But here’s another question that I still stuck on.

My intention is that after I running the program, firstly it would show the ‘enter your score’, after I type a score in it, for example ‘0.9’, the program would automatically give me the grade, which in this case, grade ‘a’.

Could you please tell me how should I do to achieve my intention?
Thanks for you time!

Tim Cameron provided the full code for the function (I copied his code below and added ‘=’ to score>=0.9 ). Since you are doing this as a learning exercise, go through the code line by line to make sure you understand how it works.

I have left the other code for you to do:

  • Instruction to get input value from user.
  • Function call that sends the entered value to the function.
  • what to do with the returned value (store it to a variable, print it immediately, etc.
def computegrade(score):
    if score>=0.9:
        return 'a'
    elif 0.8<=score<0.9:
        return 'b'
    elif 0.7<=score<0.8:
        return 'c'
    elif 0.6<=score<0.7:
        return 'd'
    elif score<0.6:
        return 'failed'
    else:
        return 'no answer'

thanks!As I followed the instruction which you listed above, I FINALLY managed to get what I want!

Here’s the code below

def compute_grade(score):
    if score > 0.9:
        return'a+'
    if score == 0.9:
        return 'a'
    elif 0.8 <= score < 0.9:
        return 'b'
    elif 0.7 <= score < 0.8:
        return 'c'
    elif 0.6 <= score < 0.7:
        return 'd'
    elif score < 0.6:
        return 'failed'
    else:
        return 'no answer'


value = input('enter your score')
score = float(value)
print(compute_grade(score))

Now I understand how to get input value from user and send the entered value to the function!

Thanks!

Great! Just some notes:

Notice that when the execution comes to this statement:

    elif 0.8 <= score < 0.9:
        return 'b'

you already know that the condition score < 0.9 will be always True because in the previous if branches you already eliminated score > 0.9 and score == 0.9. This means you can simplify the condition by removing the redundant part. I switched the order of operands for better readability:

    elif score => 0.8:
        return 'b'

The same holds for the following conditions. Also it is probably better to keep using consistently elif (or if - in your program the same result).

Also notice that the last else branch can be reached only by special float values like float('nan') (not a number).

From this description I would say that function does not have to handle non-qualifying grades. This makes sense because validation of input data and grading are different tasks.

Instead of if…elif one can use alternative approach with zip:

def compugrade(score):
    breakpoints = [0.6, 0.7, 0.8, 0.9]
    grades = ['F', 'D', 'C', 'B']
    for point, grade in zip(breakpoints, grades):
        if score < point:
            return grade
    else:
        return 'A'

for score in [0.2, 0.59, 0.6, 0.91, 1.01]:
    print(f'{score}: {compugrade(score)}')

0.2: F
0.59: F
0.6: D
0.91: A
1.01: A

So no input validation. It’s simple to add, but unclear what should happen if non-valid grade is provided. Is grade larger than 1.0 invalid?

It’s only a guess, but I think the OP should be converting to a percentage, in which case 1.0 (100%) would be the maximum.

It is dangerous to split data belonging together. Updating them can introduce errors. Here individual values of breakpoints and grades should be together.

List of tuples would be an ideal structure for this but it is a little bit hard to read and edit by human:

grades = [('F', 0.6), ('D', 0.7), ('C', 0.8), ('B', 0.9)]

We also have dict which keeps order. I think dict is OK for this use and it is considerably easier to read and edit:

def compugrade(score):
    grades = {'F': 0.6, 'D': 0.7, 'C': 0.8, 'B': 0.9}
    for grade, breaking_point in grades.items():
        if score < breaking_point:
            return grade
    else:
        return 'A'
1 Like

Thanks for the reminder!