for this while, try and except code I need to not only get the average but also
ID: 3847565 • Letter: F
Question
for this while, try and except code I need to not only get the average but also the number of times the floating point value was entered via len, the sum, sort the list of numbers and find the minimum and maximum. I was able to do this in a for loop but can't seem to include this in a while, try, and except without error. Basically, how do I merge the concepts from the for loop below into the while, try, and except code. I'm getting an error: TypeError: object of type 'float' has no len() just when I attempt to get the len.
Here is the for loop that I can't use b/c I need an while, try and exception :
_____________________________________________________________________________________________________________________________________
Here is the beginning of the while, try, and except which I need the length, sum, min and max concepts merged into:
total = 0.0
count = 0
value= 0.0
while value is not None:
try:
value = float(input('Enter a floating point value :'))
total += value
count += 1
except:
avg = total / count
print('Average is ' + str(avg))
value =None
Explanation / Answer
#!/usr/local/bin/python3
def main():
total = 0.0
count = 1
value = 0.0
n = int(input('Please enter the amount of numbers to input for finding their average: '))
while count <= n:
try:
value = float(input('Enter a floating point value : '))
total += value
if count == 1:
max = min = value
if value > max:
max = value
if value < min:
min = value
count += 1
except:
print('Error: Looks like the numbers you entered are not floating values, please enter correct values')
avg = total / count
print('Sum is ' + str(total))
print('Average is ' + str(avg))
print('Max is ' + str(max))
print('Min is ' + str(min))
if __name__=='__main__':
main()
Execution and output:
Unix Terminal> python3 correct.py
Please enter the amount of numbers to input for finding their average: 4
Enter a floating point value : 10.4
Enter a floating point value : 1.3
Enter a floating point value : 5.4
Enter a floating point value : 6.89
Sum is 23.990000000000002
Average is 4.798
Max is 10.4
Min is 1.3
Unix Terminal> python3 correct.py
Please enter the amount of numbers to input for finding their average: 3
Enter a floating point value : 1
Enter a floating point value : 2
Enter a floating point value : 3
Sum is 6.0
Average is 1.5
Max is 3.0
Min is 1.0
Unix Terminal> python3 correct.py
Please enter the amount of numbers to input for finding their average: 3
Enter a floating point value : slkjdfalfkjasf
Error: Looks like the numbers you entered are not floating values, please enter correct values
Enter a floating point value : 2.3
Enter a floating point value : 4.5
Enter a floating point value : 9.3
Sum is 16.1
Average is 4.025
Max is 9.3
Min is 2.3
Explanation:
First of all i want to give you a little knowledge on the try and catch in python. Try block is used to write the code where you expect some kind of exception can occur. Whenever there is an exception it actually go to the corresponding catch block and displays the appropriate message we have written. For example in this case whenever i enter a string when it asks for a floating point value it goes to the catch block and prints the message i have written because what i am doing is wrong. The catch block is there to handle the exceptions your code can create. You can write specific exceptions to catch like DivisionbyZero, ValueError etc and if you did not write anything every exception like in our case will fall to the catch block.
In your example you have written computing average in the catch block which is wrong because the catch block executes only when an exception occurs. Otherwise your average never gets calculated. I have picked your code and modified according to your requirement. Also has shown the output of the code. Please check.
Another point is "while value is not None" you cannot write like this in this context because you want to iterate taking floating point numbers to a certain N number like you did in your for loop example. So you have to read that N before proceeding to the while loop. Also the "value is not None" is used while handling lists or some other usecase but not here.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.