15,742,120 members
See more:
It will add all the values together and return the result
It will error if the inputted values are not ALL integers or ALL floats. So, mixed integers or floats will cause an error. It can also take exactly one argument that is a function pointer.
If the argument is a function pointer, it calls that function multiple times to get each value to add. The function pointer must not take any parameters.
Write code to test all generic cases of calls to AddAll.

What I have tried:

sum = 0
count = 0
count1 = 0
b = len(args)
for i in args: # Counts amount of int/floats
if type(i) == int:
count += 1
elif type(i) == float:
count1 += 1
print(count, count1)

for i in args: # Adds up all the values together
if i in args:
sum += (i)
return sum

Posted
Updated 20-Feb-23 23:15pm
OriginalGriff 20-Feb-23 14:26pm
And?
What does it do that you didn't expect, or not do that you did?
Where are you stuck?
What help do you need?

Solution 2

You should validate, as requested, the input values.
Try, as starting point
Python
def GetValue():
return int(input())

sum = 0
count_int = 0
count_float = 0
count_fun = 0
count_arg = len(args)
for i in args: # Counts amount of int/floats
t = type(i)
if t is int:
count_int += 1
elif t is float:
count_float += 1
elif callable(i):
count_fun += 1
else:
print("error not allowed type " + t.__name__)

if count_int > 0 and count_int != count_arg:
print("error, not ALL int")
return -1
elif count_float > 0 and count_float != count_arg:
print("error, not ALL int")
return -1
elif count_fun> 0 and count_fun != 1:
print("error, just ONE function pointer allowed")
return -1

if count_fun == 0:
for i in args: # Adds up all the values together
if i in args:
sum += (i)
else:
print("to do: handle the function pointer case")

return sum

Solution 1

Change the call to AddAll as follows:
Python
total = AddAll(1, 2, 3, 4)
print(F"{total = }")