Python Code Optimization for Finding Average Excluding Minimum and Maximum Values
There are a few issues with the code:
- The syntax for the
forloop is incorrect. It should befor i in range(4):instead offor i in 4:. - The variables
'lowest','second', and'x'need to be declared as global variables inside thecount()function if you want to modify their values. - The variables
'lowest','second', and'x'need to be passed as parameters to thecount()function if you want to update their values and access them outside the function. - The calculation at the end should be
(x - lowest - second) / 2instead of(x - lowest - second) / 2.
Here's the corrected code:
lowest = 100000000000000000000000000000000
second = 100000000000000000000000000000000
x = 0
def count():
global lowest, second, x
a = float(input('Enter the number: '))
if lowest > a:
second = lowest
lowest = a
elif second > a:
second = a
x = x + a
for i in range(4):
count()
print((x - lowest - second) / 2)
Note: The i variable is not being used in the code, so it can be removed if not needed.
原文地址: https://www.cveoy.top/t/topic/qtAu 著作权归作者所有。请勿转载和采集!