Python code- doesn't work

hello,

I just started to learn Python and I really need to understand why this code doesn’t return anything:

def scores_to_rating(score1,score2,score3,score4,score5):
    score1 = convert_to_numeric(score1)
    score2 = convert_to_numeric(score2)
    score3 = convert_to_numeric(score3)
    score4 = convert_to_numeric(score4)
    score5 = convert_to_numeric(score5)
    min_score = min(score1, score2, score3, score4, score5)
    max_score = max(score1, score2, score3, score4, score5)
    sum_of_all_five_scores = score1 + score2 + score3 + score4 + score5
    sum_of_three_middle_scores = sum_of_all_five_scores - min_score - max_score
    average_score =  sum_of_three_middle_scores/3
    rating = average_score
    return rating
    print(scores_to_rating(10,20,30,40,50))

Do you ident your code? It’s necessary in Python.

1 Like

Yes I did. Otherwise it doesn’t work. Sorry to copy paste here like that.

I understand what you are talking about. Thank you so much. Though still surprising with why is it so important space thing in Python.

1 Like

Just delete this code
score1 = convert_to_numeric(score1) score2 = convert_to_numeric(score2) score3 = convert_to_numeric(score3) score4 = convert_to_numeric(score4) score5 = convert_to_numeric(score5)
and it will works.

1 Like

There are no braces or semi colons to denote the end of a statement or block in the python language. The interpreter uses the spaces and indentations instead of those things. That is why it’s important in python.
Also if you prefix 4 spaces before each line of your code it will format it on this sub.

This line has 4 spaces before it so I can now write code.

 def some(python):
   blah blah
1 Like

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

markdown_Forums

2 Likes

The print is probably outside though.

If it isn’t, that explains why nothing gets printed.

You are not doing anything with the return value. You could get rid of the first 5 lines of the function, and the print call at the end.
Below the function (but at the same indentation level as the function’s def statement, add
Avg = scores_to_rating(10,20,30,40,50)
print(Avg)

That way you are assigning the return value to the variable Avg, then printing the result.

Above is explained how to print returned value assigned to variabile.

To print it from inside of function put print statement before return. Remeber that return end function. Because reason for function is tu run code and eventually return result. If result is returned, no more code is processed…

This can be tricky if return is inside loop so use return wisely :slight_smile:

From what I see in the code posted, you are trying to declare a function, compute a return value and then return it. All that should work ok, but there are calls to functions in there that are not yet defined. Assuming they are defined somewhere else in other code that is not pasted, the reason nothing is printed (no errors or even anytyhing else) is because that final line where you try to print the value returned by the function call is indented to be within the code block of the function itself.

When you run this the interpreter sets up the function and ends at that point. (since the last line is indented to the level of the function definition it is part of the function). If you take away the tab or spaces in front of that last line… it will then make the call to the function and attempt to print the returned result. At which point you should get errors if those other function calls are not yet defined in some other code.

It’s not at all clear how the code looks like. The indentation was done by a mod later. See the futile first half of the comments …

I’m in the same class and I’m pretty sure it’s because we haven’t actually defined what the 5 scores are in the code - yet I’m at a loss for how to do that correctly because my attempts so far come back with no output as well

Hi macikgoz,
I think the problem is the print statement ,
Remove the indentation so that print statement aligns with def statement, everything else is all right!!

1 Like

Hello, this is the code I wrote for you. Hope this will help you out :smile:
Also, your error might be calling the function within the function. The print should not be inside the function.

#CREATING A FUNCTION 
def score_to_rating (score1, score2 , score3 , score4 , score5):
    #NEED THIS TO USE BUILT IN FUNCTION SUM
    list_of_numbers = [score1, score2 , score3 , score4 , score5]
    #CONVERTS THE NUMBERS TO INT
    score1 = int(score1)
    score2 = int(score2)
    score2 = int(score3)
    score4 = int(score4)
    score5 = int(score5)
    #THE MIN AND MAX SCORE OF NUMBERS
    min_score = (min(score1, score2 , score3 , score4 , score5))
    max_score = (max(score1, score2 , score3 , score4 , score5))
    #FINDING YOUR TOTALS AND OTHER CALCULATIONS
    sum_of_numbers = sum(list_of_numbers)
    sum_of_three_middle_scores = (sum_of_numbers - min_score - max_score)
    average_score = (sum_of_three_middle_scores/3)

    print ("THIS IS YOUR MIN SCORE " + str(min_score))
    print ("THIS IS YOUR MAX SCORE " + str(max_score))
    print ("THIS IS YOUR SUM " + str(sum_of_numbers))
    print ("THIS IS YOUR ANOTHER CALCULATION " + str(sum_of_three_middle_scores))
    print ("THIS IS YOUR TOTAL DIVIDED BY THREE " + str(average_score))
    
#CALLING THE FUCNTION HERE USING PYTHON 3 AND SPYDER TEXT EDITOR FROM ANACONDA NAVIGATOR
#HAPPY CODING!!      
score_to_rating(1,2,3,4,5)    

Ok So… First scores_to_rating is a function and the last line in your code needs to be outside the function because the last line is basically calling your scores_to_rating function by passing 5 score values.

However there is another change you need to make before you can run this code. you either need to create another function called convert_to_numeric or if you do not want then you will need to remove the below lines from your score_to_rating function:-

score1 = convert_to_numeric(score1)
score2 = convert_to_numeric(score2)
score3 = convert_to_numeric(score3)
score4 = convert_to_numeric(score4)
score5 = convert_to_numeric(score5)

try this

import numpy as np
def get_Input():
return np.array([int(input(f’enter core for test {i+1} ')) for i in range(int (input (‘how many scores would you like to enter?’)))])
def print_Output(nums):
print(f" Total of all test is {len(nums)} is {np.sum(nums)} \n"
f"The highest score is{np.max(nums)} \n"
f"The lowest score is {np.min (nums)}\n"
f"The average score was {np.mean(nums)}\n"
f"The total of the middle three scores is {np.sum(nums)-(np.min(nums)+np.max(nums))}")
if name == ‘main’:
nums=get_Input()
print_Output(nums)

use def main()
if you dont know if name =