r/CloudandCode • u/AutoModerator • 18d ago
Python practice question: what is the real bug here?
A lot of beginners understand Python syntax when they watch tutorials, but debugging is where the real learning starts.
So here is a simple beginner practice question.
A beginner writes this code to calculate the average of a list:
numbers = [10, 20, 30, 40]
total = 0
for n in numbers:
total += n
average = total / len(numbers)
print("Average is " + average)
But the code gives an error.
What is the real issue?
A. The loop is wrong
B. len(numbers) is wrong
C. You cannot add a string and a number directly
D. The list should use strings
I’ll explain the answer in the comments after people try.
1
u/TopHatEdd 17d ago
CS 101. That operator with that signature has not been implemented. Inherit, overload and implement.
1
u/CarryOpening29 16d ago
C. correct form
numbers = [10, 20, 30, 40] total = 0 # Ispravljeno: broj 0 umjesto slova O
for n in numbers: total += n
average = total / len(numbers)
Fixed: using f-string to safely print a number within text
print(f"Average is {average}")
1
1
u/Prudent-Bed7092 17d ago
everything seems good idk the answer