208k views
1 vote
My code doesn't give me the right output: I have to use the Python input command, and the number could be a decimal fraction, such as 6.5, so I should use floating point numbers?

I need to have a loop that repeats six times, with a routine in the middle of the loop to get the user input as a floating point number and add the number to a sum.

Can someone check it for me?
score_list = [ input("Judge No {} :score ".format(num+1)) for num in range(6)]
int_accumilator :int = 0
for score in score_list:
if(score > 10 ):
print("i only like numbers within the 0-10 range !")
exit()
int_accumilator+= score

print("the average score is ...", int_accumilator/ len(score_list))

My code doesn't give me the right output: I have to use the Python input command, and-example-1
User Vahe
by
7.0k points

2 Answers

2 votes
There are a few issues with your code.

First, the input function returns a string, not a number. You need to convert the input to a number using the float function before adding it to the int_accumulator.

Second, you have defined int_accumulator as an int, but you are trying to add floating point numbers to it. This will cause an error. Instead, you should define int_accumulator as a float to allow it to hold decimal values.

Finally, you have a typo in your code - int_accumilator should be int_accumulator.

Here is the corrected code:

score_list = [float(input("Judge No {} :score ".format(num+1))) for num in range(6)]
int_accumulator: float = 0
for score in score_list:
if score > 10:
print("I only like numbers within the 0-10 range !")
exit()
int_accumulator += score

print("the average score is ...", int_accumulator / len(score_list))

This should allow you to get the correct output.
User Thanh Pham
by
7.0k points
3 votes

Answer:

def get_score(judge_num: int) -> float:

"""

Asks the user for the score of a single judge and returns it.

"""

score = float(input(f"Score for Judge {judge_num}: "))

return score

def calculate_average(scores: list[float]) -> float:

"""

Calculates the average score given a list of scores.

"""

return sum(scores) / len(scores)

# Initialize the total score to 0

total_score = 0

# Initialize an empty list to store the scores of the judges

scores = []

# Loop through each judge

for judge_num in range(1, 7):

# Get the score for the current judge

score = get_score(judge_num)

# Add the score to the total score

total_score += score

# Append the score to the list of scores

scores.append(score)

# Calculate the average score

average_score = calculate_average(scores)

# Print the average score

print(f"The average score is: {average_score:.2f}")

User Margarita
by
7.5k points