私はuser_input.lower()を追加しましたが、このコードはW、M、Sの入力として期待される出力を与えません。なぜ誰かが私の説明をしてくれますか?PythonでStringメソッドの概念を適用するには?
# 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')
正確に何を期待しているのか明確にする必要がありますが、string.lowerは新しい文字列を返します。変更している文字列は変更されません。文字列は不変です。一度作成すると変更することはできません。 また、あなたは==そうではありません。オブジェクトアイデンティティーのチェックが等価でないかどうか。 's' == 's'はTrue、 's'は 's'はFalseです。 –