Why doesn't this code work?? (SOLVED) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

19th Sep 2020, 5:05 PM
Sosig.py
Sosig.py - avatar
7 Answers
+ 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
19th Sep 2020, 5:07 PM
Rice Noodle
Rice Noodle - avatar
+ 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')
19th Sep 2020, 6:46 PM
Lothar
Lothar - avatar
+ 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') 🙂
20th Sep 2020, 3:31 AM
David Ashton
David Ashton - avatar
+ 4
Thanks David, i was not aware of this way using a single print statement with a ternary. Great! 👍
20th Sep 2020, 10:02 AM
Lothar
Lothar - avatar
+ 2
Kode Krasher Thanks for showing me this, I watched a few videos about this and will learn it soon.
19th Sep 2020, 7:47 PM
Sosig.py
Sosig.py - avatar
0
Thanks it works
19th Sep 2020, 5:10 PM
Sosig.py
Sosig.py - avatar
0
No problem
19th Sep 2020, 5:11 PM
Rice Noodle
Rice Noodle - avatar