Why doesn't this code work?? (SOLVED) | SoloLearn: Learn to code for FREE!


Why doesn't this code work?? (SOLVED)

I want to check if the "password" has a number, but the code says it has the number even tho it doesn't https://code.sololearn.com/cDZZ0d5zOaK5/?ref=app

9/19/2020 5:05:03 PM


11 Answers

New Answer


password = "Numb4r" if "1" in password or "2" in password or "3" in password: print("Has a number") else: print("Doesn't have a number") Sorry if i am wrong :D


This task seems to be good to apply any(): password = "Num4b" print('number found') if any(i.isdigit() for i in password) else print('no number found')


Lothar you can shorten that a bit with a ternary: print('number found' if any(i.isdigit() for i in password) else 'no number found') 🙂


Sosig.py it may be a bit advanced right now, but regular expression (regex) is a perfect candidate for this exercise. regex, for the most part, works the same across multiple languages, so it is worth learning... These links should be useful to others also?! from FREE online python book (you can buy a hardcopy on Amazon as well): https://automatetheboringstuff.com/2e/chapter7/ Video from Awesome YouTube channel: https://youtu.be/K8L6KVGG-7o Happy Coding! 😎👍


Thanks David, i was not aware of this way using a single print statement with a ternary. Great! 👍


You can also make use of functions if password.isalnum() and not password.isalpha(): print("Number found ") else: print("No number")


David Ashton Lothar you guys are truly awesome!!! 😎👍


Kode Krasher Thanks for showing me this, I watched a few videos about this and will learn it soon.


Sosig.py both those links have ALOT MORE to offer. You might bookmark them and use them as reference: https://automatetheboringstuff.com https://automatetheboringstuff.com/files/ https://m.youtube.com/watch?list=PL-osiE80TeTskrapNbzXhwoFUiLCjGgY7&v=YYXdXT2l-Gg 😎👍


Thanks it works


No problem