Home > other >  How do I resolve: TypeError: unsupported operand type(s) for : 'int' and 'list'
How do I resolve: TypeError: unsupported operand type(s) for : 'int' and 'list'

Time:09-09

The issue occurs on line 70 - The function is def test()

if sum(characters) > [1]: TypeError: unsupported operand type(s) for : 'int' and 'list'

For reference, this is a password strength checker.

How do I go about solving this? It seems like the code executes and can count the length of the input as well as add points to the score

import sys
import time
import random
import string  # check for certain characters


def ten():
    length = 10
    chars = string.ascii_letters   string.digits   '!@#$%^&*()'

    rnd = random.SystemRandom()
    print(''.join(rnd.choice(chars) for i in range(length)))


def fifteen():
    length = 15
    chars = string.ascii_letters   string.digits   '!@#$%^&*()'

    rnd = random.SystemRandom()
    print(''.join(rnd.choice(chars) for i in range(length)))


def twenty():
    length = 20
    chars = string.ascii_letters   string.digits   '!@#$%^&*()'

    rnd = random.SystemRandom()
    print(''.join(rnd.choice(chars) for i in range(length)))


def twentyfive():
    length = 25
    chars = string.ascii_letters   string.digits   '!@#$%^&*()'

    rnd = random.SystemRandom()
    print(''.join(rnd.choice(chars) for i in range(length)))


def test():
    # Password Checker

    option = input()

    upper_case = ([1 if c in string.ascii_uppercase else 0 for c in option])
    lower_case = ([1 if c in string.ascii_lowercase else 0 for c in option])
    special = ([1 if c in string.punctuation else 0 for c in option])
    digits = ([1 if c in string.digits else 0 for c in option])

    characters = [upper_case, lower_case, special, digits]
    length = len(option)
    score = 0

    if length > 8:
        score  = 1
    if length > 12:
        score  = 1
    if length > 13:
        score  = 1
    if length > 15:
        score  = 1
    if length > 16:
        score  = 1
    if length > 20:
        score  = 1
    if length > 25:
        score  = 1

    print(f"Password length is {str(length)}, adding {str(score)} points!")

    if sum(characters) > 1:
        score  = 1
    if sum(characters) > 2:
        score  = 1
    if sum(characters) > 3:
        score  = 1

    print(f"Password has {str(sum(characters))} different character types, adding {str(sum(characters) - 1)} points! ")

    if score < 4:
        print(f"Week {str(score)} / 7")
    elif score == 4:
        print(f"Week {str(score)} / 7")
    elif 4 < score < 6:
        print(f"Week {str(score)} / 7")
    elif score > 6:
        print(f"Week {str(score)} / 7")


def quit():
    print("This program will now exit...")
    time.sleep(2)
    sys.exit()


# This is the menu function

def menu():
    print("Hello Welcome To My Password Generator!!!")
    time.sleep(1)

    option = input("""
    
                        1: Generate a Random 10 Character Password
                        2: Generate a Random 15 Character Password
                        3: Generate a Random 20 Character Password
                        4: Generate a Random 25 Character Password
                        5: Test Password Strength
                        Q: Quit
                        
                        Please decide on an option from 1, 2, 3, 4, 5 or Q:
                        
    """)

    if option == "1":
        ten()
    elif option == "2":
        fifteen()
    elif option == "3":
        twenty()
    elif option == "4":
        twentyfive()
    elif option == "5":
        test()
    elif option == "Q" or option == "q":
        quit()
    else:
        print("Error, you must enter a valid choice from the above menu")
        menu()


menu()

CodePudding user response:

I've redone your file to get rid of the unnecessary function calls, to eliminate the silly recursion, and to check the character classes in a better way. This does what you want, I believe.

import sys
import random
import string

alphabet = string.ascii_letters   string.digits   '!@#$%^&*()'

def generate(length):
    print(''.join(random.choice(alphabet) for i in range(length)))

def test():
    # Password Checker

    option = input()

    upper_case = any(c in string.ascii_uppercase for c in option)
    lower_case = any(c in string.ascii_lowercase for c in option)
    special = any(c in string.punctuation for c in option)
    digits = any(c in string.digits for c in option)

    length = len(option)
    score = 0

    for k in [8,12,13,15,16,20,25]:
        if length < k:
            break
        score  = 1

    print(f"Password length is {length}, adding {score} points!")

    characters = sum([upper_case, lower_case, special, digits])
    score  = characters - 1

    print(f"Password has {characters} different character types, adding {characters-1} points! ")

    if score < 4:
        print(f"Weak {score} / 7")
    elif score < 5:
        print(f"OK {score} / 7")
    elif score < 6:
        print(f"Medium {score} / 7")
    else:
        print(f"Strong {score} / 7")


# This is the menu function

def menu():
    print("Hello Welcome To My Password Generator!!!")

    while True:
        option = input("""
                            1: Generate a Random 10 Character Password
                            2: Generate a Random 15 Character Password
                            3: Generate a Random 20 Character Password
                            4: Generate a Random 25 Character Password
                            5: Test Password Strength
                            Q: Quit
                            
                            Please decide on an option from 1, 2, 3, 4, 5 or Q:
        """)

        if option == "1":
            generate(10)
        elif option == "2":
            generate(15)
        elif option == "3":
            generate(20)
        elif option == "4":
            generate(25)
        elif option == "5":
            test()
        elif option in "Qq":
            break
        else:
            print("Error, you must enter a valid choice from the above menu")

menu()

CodePudding user response:

characters = [upper_case, lower_case, special, digits]

creates a list of lists. sum(characters) won't work because that tries to add the lists to 0 to get the total, and you can't add a list to a number.

You should use

characters = upper_case   lower_case   special   digits

to concatenate all the lists instead of making nested lists.

CodePudding user response:

In my case, with the original posted code, I was missing the any() function.

upper_case = any([1 if c in string.ascii_uppercase else 0 for c in option])
lower_case = any([1 if c in string.ascii_lowercase else 0 for c in option])
special = any([1 if c in string.punctuation else 0 for c in option])
digits = any([1 if c in string.digits else 0 for c in option])

Thank you for the help.

  • Related