I have the following code and while it works for just checking one function (say the lower case), it stops working when two conditions are introduced. Could someone shed some light for teaching purposes, on why this is the case, give a solution, and explain the rationale. Note: this is not a duplicate of any other question, as the other examples don't explicitly deal with string boolean commands.
Expected outcome: The email address should only be generated IF the username input is lowercase AND contains digits. (e.g. username123 is acceptable, USER is not, USER123 is not)
The solution/answer must be simple and ideally not include a rewrite of the code (i.e without rewriting the function and introducing parameters etc, if this as possible)
Code:
def main():
print("*************************** Create an e-mail address*********************")
print("*************************** ********************** ")
username=input("Please enter your desired username:" )
if username.islower() and username.isdigit():
email=username+"@gmail.com"
print("Your unique email address is now:", email)
else:
print("Your username needs to be lower case ...............")
main()
main()
As mentioned, the below code (checking for just the islower) works ....
if username.islower():
email=username+"@gmail.com"
print("Your unique email address is now:", email)
else:
print("Your username needs to be lower case ...............")
main()
Working output:
*************************** Create an e-mail address*********************
*************************** **********************
Please enter your desired username:USER
Your username needs to be lower case ...............
*************************** Create an e-mail address*********************
*************************** **********************
Please enter your desired username:user
Your unique email address is now: user@gmail.com
>>>
UPDATE: Someone suggested it was due to the missing (), but this isn't the case:
def main():
print("*************************** Create an e-mail address*********************")
print("*************************** ********************** ")
username=input("Please enter your desired username:" )
if username.islower() and username.isdigit():
email=username+"@gmail.com"
print("Your unique email address is now:", email)
else:
print("Your username needs to be lower case ...............")
main()
main()
Erroneous output:
*************************** Create an e-mail address*********************
*************************** **********************
Please enter your desired username:USER
Your username needs to be lower case ...............
*************************** Create an e-mail address*********************
*************************** **********************
Please enter your desired username:user
Your username needs to be lower case ...............
*************************** Create an e-mail address*********************
*************************** **********************
Please enter your desired username:user123
Your username needs to be lower case ...............
*************************** Create an e-mail address*********************
*************************** **********************
Please enter your desired username:
Removing the recursive main() call doesn't change things either:
def main():
print("*************************** Create an e-mail address*********************")
print("*************************** ********************** ")
username=input("Please enter your desired username:" )
if username.islower() and username.isdigit():
email=username+"@gmail.com"
print("Your unique email address is now:", email)
else:
print("Your username needs to be lower case and contain digits...............")
main()
Erroneous output:
Please enter your desired username:USERNAME
Your username needs to be lower case and contain digits...............
>>> main()
*************************** Create an e-mail address*********************
*************************** **********************
Please enter your desired username:username123
Your username needs to be lower case and contain digits...............
>>>
Using OR instead of AND does not work either
Output below, when changed to OR
Please enter your desired username:USERNAME
Your username needs to be lower case and contain digits...............
>>> main()
*************************** Create an e-mail address*********************
*************************** **********************
Please enter your desired username:username
Your unique email address is now: username@gmail.com
>>>
Trying another suggested answer below using set(range(10)).intersection:
The following - still not working:
def main():
print("*************************** Create an e-mail address*********************")
print("*************************** ********************** ")
username=input("Please enter your desired username:" )
#if username.islower() or username.isdigit():
if username.islower() and set(range(10)).intersection(username):
email=username+"@gmail.com"
print("Your unique email address is now:", email)
else:
print("Your username needs to be lower case and contain digits...............")
main()
Output
>>> main()
*************************** Create an e-mail address*********************
*************************** **********************
Please enter your desired username:user
Your username needs to be lower case and contain digits...............
>>> main()
*************************** Create an e-mail address*********************
*************************** **********************
Please enter your desired username:username123
Your username needs to be lower case and contain digits...............
>>>