Help
Task: Write a program that takes in a string as input and evaluates it as a valid password. The password is valid if it has at a minimum 2 numbers, 2 of the following special characters ('!', '@', '#', '$', '%', '&', '*'), and a length of at least 7 characters. If the password passes the check, output 'Strong', else output 'Weak'. My code: password = input() x = "" y = "" for i in password : if i.isnumeric(): x = x + i if i == '!' or '@' or '#' or '$' or '%' or '&' or '*': y = y + i if len(password) >=7 and len(x) >= 2 and len(y) >= 2 : print("Strong") else: print("Weak") I got all the cases correct except case number 8 Anyone knows the problem?
7/2/2022 11:54:42 PM
Jassim Id Charif
5 Answers
New AnswerJassim Id Charif , allow me to give 2 comments to your code: => using isnumeric(): you may be surprised, but isnumeric() does not only accept digits from 0 -> 9, but also fractions in various languages. password = "꠵↉୴¼#@asdf" will be cosidered as "Strong" by your code -> we should use isdigit() instead. this accepts only digits from 0 -> 9. => 2 if starements: it would be better to use elif ... instead of the second if
Korkunç the Terrible Frusciante no problem everything was clear . I think they updated it cuz now its 7 char
Hello, I deleted my reply here because it was severely lacking. I put a link for a much better explanation. i == "#" or i == "&" or... : part still applies. That you can instead do: if i in "#&!%@": still applies too. But Python logic is not exactly like normal logic due to parsing. Please have a look at Per Bratthammar reply in this: https://www.sololearn.com/Discuss/3056739/?ref=app The more relevant info is or getting evaluated lazily. That's what makes things different. Today something RW said in the below discussion reminded me of your post (anything that's 0 evaluating to False). Well that means, as PB said in his post, (), "", {}, [] evaluate to False. That shouldn't have been left unmentioned either. (I'd just mentioned "not True" and 0) https://www.sololearn.com/Discuss/3057633/?ref=app As a side note: This was irresponsible. I'll refrain from replying questions. BTW, please take note of Brian's short circuit remark on why parantheses might not always work with "or".
Not at all, and sorry for the typos :) I thought this exercise had max char restriction too, though, 14 or something?