-3

For my code, I am trying to find the number of times 4 or 5 occurs in each of the three lists, then printing out the maximum value of the most number of times 4 or 5 occurs.

For example, 4 or 5 occurs in the list AAAAA 8 times, also 8 times for BBB and 5 times for K. So the last line of the code should print 8, but I get an error shown below my code as shown.

Note: I have to make this code using functions with parameters in them. I am familiar with lists, strings, for loops, and some other stuff. So don't show me new stuff cause I'm a beginner.

Code:

AAAAA = [4, 2, 1, 2, 4, 2, 4, 4, 5, 2, 2, 1, 5, 2, 4, 3, 1, 1, 3, 3, 5]
BBB = [5, 2, 1, 2, 4, 5, 4, 4, 1, 2, 2, 2, 4, 4, 4, 3, 1, 2, 3, 3, 2]
K = [4, 1, 2, 1, 2, 1, 2, 5, 1, 1, 1, 1, 4, 2, 2, 1, 5, 1, 3, 4, 1]

x = 4
y = 5

def four_or_five(AAAAA, x): 
    count = 0
    for ele in AAAAA: 
        if (ele == x): 
            count = count + 1
    return count
def four_or_five(AAAAA, y): 
    count = 0
    for ele in AAAAA: 
        if (ele == y): 
            count = count + 1
    return count

res = print(four_or_five(AAAAA, x) + four_or_five(AAAAA, y)) ### 4 and 5's in list AAAAA


def four_or_five(BBB, x): 
    count = 0
    for ele in BBB: 
        if (ele == x): 
            count = count + 1
    return count
def four_or_five(BBB, y): 
    count = 0
    for ele in BBB: 
        if (ele == y): 
            count = count + 1
    return count

res1 = print(four_or_five(BBB, x)+four_or_five(BBB, y))  ### 4 and 5's in list BBB


def four_or_five(K, x): 
    count = 0
    for ele in K: 
        if (ele == x): 
            count = count + 1
    return count
def four_or_five(K, y): 
    count = 0
    for ele in K: 
        if (ele == y): 
            count = count + 1
    return count

res2 = print(four_or_five(K, x)+four_or_five(K, y))      ### 4 and 5's in list K


max_res = max(res,res1,res2)            ### Find the max number of times 4 and 5 occurs, which is 8
print(max_res)                              
_____________________________________________________________
OUTPUT:
8
8
5
TypeError: '>' not supported between instances of 'NoneType' and 'NoneType'

Note: Do not worry about certain digits occuring, 4 or 5 is supposed to be fixed so just leave it as 4 or 5. Also when I say don't alter my code's format, I mean as in leave it as defined functions with parameters in them.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Yes I know the code is kind of long, but it's just 6 function definitions, 2 for each of the three lists; one counting 4's and one counting 5's –  Dec 02 '19 at 04:37
  • 1
    Your function for counting 4 and function for counting 5 has the same name, So the second actually overwrites the first. Also, do you know about conditionals? You can count "4 **or** 5" together if you do. Lastly, functions are used so that you don't need to rewrite code. You seem to assume that functions would need to be written every single time you need to use one. – Paritosh Singh Dec 02 '19 at 04:38
  • Yes I know how to use conditionals. I am confused on your explanation, could you give me another hint. I don't think the function name will matter if its the same because if you scroll down, you can see it actually counted the 4's and 5's correctly: 8 8 5 –  Dec 02 '19 at 04:41
  • For my code, 8 8 5 is not needed to be printed out, I just need the max number which is 8 to be printed out. When I try to print the max value of the three numbers, it gives nonetype error –  Dec 02 '19 at 04:45
  • The reason it prints the correct outputs is because you passed the correct parameters. Which should make you realise that the same function should not have been written multiple times, once was enough. Also, the reason it gives Nonetype is because you're assigning the output of a "print" call itself. Remove the print, assign the values. Print in a different line – Paritosh Singh Dec 02 '19 at 04:48
  • You should implement **A SINGLE FUNCTION**, not 6 different functions of the exact same name (I'm not even sure how the Python interpreter has allowed this). – goodvibration Dec 02 '19 at 04:49
  • I was thinking about that when i was coding this but i dont know how to connect the lists, like i want to find the number of 4's of 5's in each list so i can compare and find the max number –  Dec 02 '19 at 04:51
  • The problem here is that you are not storing the results in memory, therefore it returns nonetype – Newton Karanu Dec 02 '19 at 04:51
  • 1
    When I tried the find max function with random numbers it worked, but I think its not working because i assigned a print statement to a variable –  Dec 02 '19 at 04:53
  • @PatrickPichart The fact that you’re assigning a `print()` statement to a variable can’t be helping, that’s for sure. Also, is your code now duplicated? All the functions have the same names. – AMC Dec 02 '19 at 06:04

2 Answers2

2

Try to create variables with more descriptive names! Here is the correct code:

def four_or_five(lst):
    return lst.count(4) + lst.count(5) # .count() is self explanatory, but below is another way which is more similar to your code:

def alt_four_or_five(lst):
    c = 0
    for i in lst:
        if i == 4 or i == 5:
            c += 1
    return c

big_list_1 = [4, 2, 1, 2, 4, 2, 4, 4, 5, 2, 2, 1, 5, 2, 4, 3, 1, 1, 3, 3, 5]
big_list_2 = [5, 2, 1, 2, 4, 5, 4, 4, 1, 2, 2, 2, 4, 4, 4, 3, 1, 2, 3, 3, 2]
big_list_3 = [4, 1, 2, 1, 2, 1, 2, 5, 1, 1, 1, 1, 4, 2, 2, 1, 5, 1, 3, 4, 1]

bl1_count = four_or_five(big_list_1)
bl2_count = four_or_five(big_list_2)
bl3_count = four_or_five(big_list_3)

result = max([bl1_count, bl2_count, bl3_count) 

print(result) # => 8

If I understand your code, your problem is that you are assigning the variables res, res1 and res2 the return value of the print() function, which is always None. You just need to assing the return value of the function itself.

Also, you don't need to declare functions everytime you use them:

### Instead of:

def func_a(x, y, z):
   return x + y + z

x = 1
y = 2
z = 3
func_a(x, y, z)

def func_a(a, b, c):
   return a + b + c

a = 4
b = 5
c = 6
func_a(a, b, c)



### Do:

def func_a(x, y, z):
    return x + y + z

x = 1
y = 2
z = 3
a = 4
b = 5
c = 6
print(func_a(x, y, z))
print(func_a(a, b, c))
Santiago M.
  • 63
  • 1
  • 9
0

You just need to remove the prints from all the res variables. Because you are trying to print these values, they wont be stored in the variable. Also, 'NoneType' in the error means that the object has nothing stored inside it. Learn more about it here

Code:

AAAAA = [4, 2, 1, 2, 4, 2, 4, 4, 5, 2, 2, 1, 5, 2, 4, 3, 1, 1, 3, 3, 5]
BBB = [5, 2, 1, 2, 4, 5, 4, 4, 1, 2, 2, 2, 4, 4, 4, 3, 1, 2, 3, 3, 2]
K = [4, 1, 2, 1, 2, 1, 2, 5, 1, 1, 1, 1, 4, 2, 2, 1, 5, 1, 3, 4, 1]

x = 4
y = 5

def four_or_five(AAAAA, x):
    count = 0
    for ele in AAAAA:
        if (ele == x):
            count = count + 1
    return count
def four_or_five(AAAAA, y):
    count = 0
    for ele in AAAAA:
        if (ele == y):
            count = count + 1
    return count

res = four_or_five(AAAAA, x) + four_or_five(AAAAA, y) ### 4 and 5's in list AAAAA


def four_or_five(BBB, x):
    count = 0
    for ele in BBB:
        if (ele == x):
            count = count + 1
    return count
def four_or_five(BBB, y):
    count = 0
    for ele in BBB:
        if (ele == y):
            count = count + 1
    return count

res1 = four_or_five(BBB, x)+four_or_five(BBB, y)  ### 4 and 5's in list BBB


def four_or_five(K, x):
    count = 0
    for ele in K:
        if (ele == x):
            count = count + 1
    return count
def four_or_five(K, y):
    count = 0
    for ele in K:
        if (ele == y):
            count = count + 1
    return count

res2 = four_or_five(K, x)+four_or_five(K, y)    ### 4 and 5's in list K


max_res = max(res,res1,res2)            ### Find the max number of times 4 and 5 occurs, which is 8
print(max_res)