+ 1

I would like to ask what's the problem in my code and why...?

def main(): numbers = [] for z in range(20): num = int(input(f"Enter number {z + 1}: ")) numbers.append(num) lowest_num = determineLowest(numbers) highest_num = determineHighest(numbers) total_sum = sum(numbers) average = total_sum / len(numbers) print(f"Lowest number: {lowest_num:.2f}") print(f"Highest number: {highest_num:.2f}") print(f"Total of the numbers: {total_sum:.2f}") print(f"Average of the numbers: {average:.2f}") def determineLowest(numbers): lowest = numbers[0] for num in numbers: if num < lowest: lowest = num return lowest def determineHighest(numbers): highest = numbers[0] for num in numbers: if num > highest: highest = num return highest if __name__=="_main_": main()

18th Apr 2024, 12:58 PM
Dumisani Kokozela
3 Réponses
+ 2
I think if you concentrate to revise your code you can remove those bugs. Very common bugs. Here are the clues: 1. Indentation 2. Use 'try-except' to handle 'EOFError' 3. Lastly check '__main__' Do it by your yourself, it can help you to find bugs in others code too. This is a part of learning.
18th Apr 2024, 1:55 PM
`ᴴᵗᵗየ
`ᴴᵗᵗየ - avatar
+ 2
There are a couple of issues in your code:The indentation for the function calls inside the main() function is incorrect. They are currently inside the loop, which means they will be executed multiple times unnecessarily. They should be outside the loop, so they are executed only once after all numbers have been input.The determineLowest() and determineHighest() functions return too early. They should only return the lowest or highest number after looping through all numbers.The if statement if __name__=="_main_": is incorrect. It should be if __name__=="__main__":.
18th Apr 2024, 2:39 PM
Kamran Mushtaq
Kamran Mushtaq - avatar
0
Dumisani Kokozela , You have a variety of bugs which makes it seem like you didn't really nail down the mechanics of each part before attempting to combine them in something bigger. If I were you, I'd break it up by commenting out major portions and testing the sub parts. Also, testing would go a lot faster if you didn't force yourself to enter 20 numbers each run. Try reducing it to 2, or even hard-code your list so you don't have to enter anything while testing. Then you can restore the input later.
19th Apr 2024, 9:40 AM
Rain
Rain - avatar