15,747,059 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:

```def AddAll(*args):
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

AddAll(1, 2, 3, 4)```
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 1

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

## 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```

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 145 Graeme_Grant 70 Richard MacCutchan 40 Ramug10 -16 markomo -32
 OriginalGriff 135 Graeme_Grant 65 Richard MacCutchan 40 Ramug10 -16

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900