在下面提到的代码中我添加了user_input.lower()但是,这段代码没有给出输入的预期输出为W,M或S,有人可以解释我为什么,我在python中错误地使用字符串方法?
# Password Generator
import random
import string
print('----------------PASSWORD GENERATOR-------------------')
print('''We will help you to generate 3 types of passwords
1. Weak passwords : It will consist 4 alphabets.
2. Moderate passwords: It will consist 7 alphanumeric characters.
3. Strong Passwords : It will consist 10 alphanumeric and symbol
characters.''')
while True:
user_input = input('''
What type of password you need?
Enter "W" for weak password
Enter "M" for moderate password
Enter "S" for strong password\n''')
# To reduce chances of error if user gives command in upper or lower case.
user_input.lower()
if user_input is 'w':
alphabet = string.ascii_letters # string.digits + string.punctuation
password = ''.join(random.choice(alphabet) for i in range(4))
print(password)
break
elif user_input is 'm':
alphabet = string.ascii_letters + string.digits # string.punctuation
password = ''.join(random.choice(alphabet) for i in range(7))
print(password)
break
elif user_input is 's':
alphabet = string.ascii_letters + string.digits + string.punctuation
password = ''.join(random.choice(alphabet) for i in range(10))
print(password)
break
else:
print('Please enter a valid choice')
答案 0 :(得分:0)
这是因为当你使用“.lower()”时,实际上并没有将输出保存到变量中。因此,要将其保存在变量中,您必须执行“user_input = user_input.lower()”。