import sys
from typing import Union
# Define types for mean function, trying to analyze input possibilities
Number = Union[int, float] # Number can be either int or float type
Numbers = list[Number] # Numbers is a list of Number types
Scores = Union[Number, Numbers] # Scores can be single or multiple
def mean(scores: Scores, method: int = 1) -> float:
"""
Calculate the mean of a list of scores.
Average and Average2 are hidden functions performing mean algorithm
If a single score is provided in scores, it is returned as the mean.
If a list of scores is provided, the average is calculated and returned.
"""
def average(scores):
"""Calculate the average of a list of scores using a Python for loop with rounding."""
sum = 0
len = 0
for score in scores:
if isinstance(score, Number):
sum += score
len += 1
else:
print("Bad data: " + str(score) + " in " + str(scores))
sys.exit()
return sum / len
def average2(scores):
"""Calculate the average of a list of scores using the built-in sum() function with rounding."""
return sum(scores) / len(scores)
# test to see if scores is a list of numbers
if isinstance(scores, list):
if method == 1:
# long method
result = average(scores)
else:
# built in method
result = average2(scores)
return round(result + 0.005, 2)
return scores # case where scores is a single valu
# try with one number
singleScore = 100
print("Print test data: " + str(singleScore)) # concat data for single line
print("Mean of single number: " + str(mean(singleScore)))
print()
# define a list of numbers
testScores = [90.5, 100, 85.4, 88]
print("Print test data: " + str(testScores))
print("Average score, loop method: " + str(mean(testScores)))
print("Average score, function method: " + str(mean(testScores, 2)))
print()
badData = [100, "NaN", 90]
print("Print test data: " + str(badData))
print("Mean with bad data: " + str(mean(badData)))
Print test data: 100
Mean of single number: 100
Print test data: [90.5, 100, 85.4, 88]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[2], line 58
56 testScores = [90.5, 100, 85.4, 88]
57 print("Print test data: " + str(testScores))
---> 58 print("Average score, loop method: " + str(mean(testScores)))
59 print("Average score, function method: " + str(mean(testScores, 2)))
61 print()
Cell In[2], line 40, in mean(scores, method)
37 if isinstance(scores, list):
38 if method == 1:
39 # long method
---> 40 result = average(scores)
41 else:
42 # built in method
43 result = average2(scores)
Cell In[2], line 24, in mean.<locals>.average(scores)
22 len = 0
23 for score in scores:
---> 24 if isinstance(score, Number):
25 sum += score
26 len += 1
File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py:710, in _BaseGenericAlias.__instancecheck__(self, obj)
709 def __instancecheck__(self, obj):
--> 710 return self.__subclasscheck__(type(obj))
File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py:713, in _BaseGenericAlias.__subclasscheck__(self, cls)
712 def __subclasscheck__(self, cls):
--> 713 raise TypeError("Subscripted generics cannot be used with"
714 " class and instance checks")
TypeError: Subscripted generics cannot be used with class and instance checks
import sys
from typing import Union
# Define types for mean function, trying to analyze input possibilities
Number = Union[int, float] # Number can be either int or float type
Numbers = list[Number] # Numbers is a list of Number types
Scores = Union[Number, Numbers] # Scores can be single or multiple
def mean(scores: Scores, method: int = 1) -> float:
"""
Calculate the mean of a list of scores.
Average and Average2 are hidden functions performing mean algorithm
If a single score is provided in scores, it is returned as the mean.
If a list of scores is provided, the average is calculated and returned.
"""
def average(scores):
"""Calculate the average of a list of scores using a Python for loop with rounding."""
sum = 0
len = 0
for score in scores:
if isinstance(score, Number):
sum += score
len += 1
else:
print("Bad data: " + str(score) + " in " + str(scores))
sys.exit()
return sum / len
def average2(scores):
"""Calculate the average of a list of scores using the built-in sum() function with rounding."""
return sum(scores) / len(scores)
# test to see if scores is a list of numbers
if isinstance(scores, list):
if method == 1:
# long method
result = average(scores)
else:
# built in method
result = average2(scores)
return round(result + 0.005, 2)
return scores # case where scores is a single valu
# try with one number
singleScore = 100
print("Print test data: " + str(singleScore)) # concat data for single line
print("Mean of single number: " + str(mean(singleScore)))
print()
# define a list of numbers
testScores = [90.5, 100, 85.4, 88]
print("Print test data: " + str(testScores))
print("Average score, loop method: " + str(mean(testScores)))
print("Average score, function method: " + str(mean(testScores, 2)))
print()
badData = [100, "NaN", 90]
print("Print test data: " + str(badData))
print("Mean with bad data: " + str(mean(badData)))
Print test data: 100
Mean of single number: 100
Print test data: [90.5, 100, 85.4, 88]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[3], line 58
56 testScores = [90.5, 100, 85.4, 88]
57 print("Print test data: " + str(testScores))
---> 58 print("Average score, loop method: " + str(mean(testScores)))
59 print("Average score, function method: " + str(mean(testScores, 2)))
61 print()
Cell In[3], line 40, in mean(scores, method)
37 if isinstance(scores, list):
38 if method == 1:
39 # long method
---> 40 result = average(scores)
41 else:
42 # built in method
43 result = average2(scores)
Cell In[3], line 24, in mean.<locals>.average(scores)
22 len = 0
23 for score in scores:
---> 24 if isinstance(score, Number):
25 sum += score
26 len += 1
File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py:710, in _BaseGenericAlias.__instancecheck__(self, obj)
709 def __instancecheck__(self, obj):
--> 710 return self.__subclasscheck__(type(obj))
File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py:713, in _BaseGenericAlias.__subclasscheck__(self, cls)
712 def __subclasscheck__(self, cls):
--> 713 raise TypeError("Subscripted generics cannot be used with"
714 " class and instance checks")
TypeError: Subscripted generics cannot be used with class and instance checks
import sys
from typing import Union
# Define types for mean function, trying to analyze input possibilities
Number = Union[int, float] # Number can be either int or float type
Numbers = list[Number] # Numbers is a list of Number types
Scores = Union[Number, Numbers] # Scores can be single or multiple
def mean(scores: Scores, method: int = 1) -> float:
"""
Calculate the mean of a list of scores.
Average and Average2 are hidden functions performing mean algorithm
If a single score is provided in scores, it is returned as the mean.
If a list of scores is provided, the average is calculated and returned.
"""
def average(scores):
"""Calculate the average of a list of scores using a Python for loop with rounding."""
sum = 0
len = 0
for score in scores:
if isinstance(score, Number):
sum += score
len += 1
else:
print("Bad data: " + str(score) + " in " + str(scores))
sys.exit()
return sum / len
def average2(scores):
"""Calculate the average of a list of scores using the built-in sum() function with rounding."""
return sum(scores) / len(scores)
# test to see if scores is a list of numbers
if isinstance(scores, list):
if method == 1:
# long method
result = average(scores)
else:
# built in method
result = average2(scores)
return round(result + 0.005, 2)
return scores # case where scores is a single valu
# try with one number
singleScore = 100
print("Print test data: " + str(singleScore)) # concat data for single line
print("Mean of single number: " + str(mean(singleScore)))
print()
# define a list of numbers
testScores = [90.5, 100, 85.4, 88]
print("Print test data: " + str(testScores))
print("Average score, loop method: " + str(mean(testScores)))
print("Average score, function method: " + str(mean(testScores, 2)))
print()
badData = [100, "NaN", 90]
print("Print test data: " + str(badData))
print("Mean with bad data: " + str(mean(badData)))
Print test data: 100
Mean of single number: 100
Print test data: [90.5, 100, 85.4, 88]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[4], line 58
56 testScores = [90.5, 100, 85.4, 88]
57 print("Print test data: " + str(testScores))
---> 58 print("Average score, loop method: " + str(mean(testScores)))
59 print("Average score, function method: " + str(mean(testScores, 2)))
61 print()
Cell In[4], line 40, in mean(scores, method)
37 if isinstance(scores, list):
38 if method == 1:
39 # long method
---> 40 result = average(scores)
41 else:
42 # built in method
43 result = average2(scores)
Cell In[4], line 24, in mean.<locals>.average(scores)
22 len = 0
23 for score in scores:
---> 24 if isinstance(score, Number):
25 sum += score
26 len += 1
File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py:710, in _BaseGenericAlias.__instancecheck__(self, obj)
709 def __instancecheck__(self, obj):
--> 710 return self.__subclasscheck__(type(obj))
File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py:713, in _BaseGenericAlias.__subclasscheck__(self, cls)
712 def __subclasscheck__(self, cls):
--> 713 raise TypeError("Subscripted generics cannot be used with"
714 " class and instance checks")
TypeError: Subscripted generics cannot be used with class and instance checks