1

I want to check if the string that is given through parameters in the function is in a list. The code itself doesn't produce any errors but it works the wrong way. If I give my function "-a" as parameter it still says that it's not in the list but it definitely is.

This is the code :

def generatePassword(pLength, mode): password = str() commands = ["-a", "-n", "-s", "-allupper", "-mixupper"] alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"] specialCharacters = ["!", "?", "&", "@", "-", "=", "#", "+", "*", "/", "%", "§"] if mode.lower().split() not in commands: print("Couldn't recognize commands...") else: for n in range(pLength): i = random.randint(1, 2) if "-a" in mode.lower().split(): password += alphabet[random.randint(0, 25)] print("[+]", password) generatePassword(30, "-a") 
11
  • 1
    Your first if line asks if a list is in another list, this will never be true for non-nested lists, even if the lists are identical Commented Oct 31, 2016 at 9:57
  • When you split a string, the output will be a list. As the list is not there in commands, it is returning False. Drop the split() to fix this. Commented Oct 31, 2016 at 9:57
  • @thefourtheye A mere dropping of spilt() is not a solution. There might be more than command. Commented Oct 31, 2016 at 10:00
  • @thefourtheye I tried dropping it but if I do so I won't be able to use more than one command.. Commented Oct 31, 2016 at 10:03
  • 1
    You don't need alphabet (use the builtin string.lowercase) or specialCharacters (use the builtin string.punctuation) Commented Oct 31, 2016 at 10:03

2 Answers 2

2

Your condition is not good:

if mode.lower().split() not in commands: print("Couldn't recognize commands...") 

Replace it by (for example):

args = set(mode.lower().split()) if not set(args).issubset(set(commands)): print("Couldn't recognize commands...") 

http://python.6.x6.nabble.com/Test-if-list-contains-another-list-td1506964.html

Sign up to request clarification or add additional context in comments.

1 Comment

This worked for me thanks! I didn't learn about sets yet so I didn't know when and how to implement them but I'll take a look at them soon.
0

You can use the any command to check if any of the words in mode.lower().split() are not in commands:

def generatePassword(pLength, mode): password = str() commands = ["-a", "-n", "-s", "-allupper", "-mixupper"] alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"] specialCharacters = ["!", "?", "&", "@", "-", "=", "#", "+", "*", "/", "%", "§"] if any(x not in commands for x in mode.lower().split()): print("Couldn't recognize commands...") else: for n in range(pLength): i = random.randint(1, 2) if "-a" in mode.lower().split(): password += alphabet[random.randint(0, 25)] print("[+]", password) generatePassword(30, "-a") 

4 Comments

This way it won't let me use more than one command in the parameter. That's why I splitted it up :/
@hudumudu Check the answer now! You can use the any command to see if any of the words in mode.lower().split() are not in commands.
This worked as well thanks! Can I accept more than one answer as correct in Stackoverflow?
@hudumudu Unfortunately not, but do up vote all that you felt were helpful (y). Actually maybe you don't have enough rep to do that but it is alright, just pick whichever answer you feel is better suited to your question. Glad to help :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.