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

+2

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

Sosig.py

11 Answers

New Answer

+12

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

+10

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')

+7

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') 🙂

+6

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! 😎👍

+4

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

+3

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

+3

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

+2

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

+2

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 😎👍

0

Thanks it works

0

No problem