Click here to Skip to main content
15,883,894 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Comments
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?

Change the call to AddAll as follows:
Python
total = AddAll(1, 2, 3, 4)
print(F"{total = }")
 
Share this answer
 
You should validate, as requested, the input values.
Try, as starting point
Python
def GetValue():
    return int(input())

def AddAll(*args):
    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
 
Share this answer
 

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



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