I am learning python and working on a password generator. My goal is to take inputs of 1. How many password options the user would like generated and 2. The character length the password needs to be. If the character length is greater than 6 I'd like to give the option for the user to have a personalized password (through answering questions and taking the answers provided to be apart of the password that is generated) otherwise a randomly generated password will be created. I am using the PyInputPlus module to help with the personalization option. I'm receiving a validation exception however when I look into the exception, i'm not seeing why my response to the menu prompt isn't being accepted. Can someone give the explanation of what i'm not doing correctly? (there are two different approaches I tried in my if statement for '1' and '2')
def personalized_pass():
questions = ['What is the name of your favorite pet? \n',
'What is the name of your favorite city? \n',
'Who is your favorite singer/band? \n',
'I dont want a personalized password, give me a random password \n'
]
print('You have selected to have a personalized password.')
ans1 = int(pyip.inputMenu(questions, prompt='Please select a question to answer: \n', numbered=True))
if ans1 == '1' or 1:
print('What is the name of your favorite pet? ')
ans1 = input()
elif ans1 == '2' or 2:
ans1 = pyip.inputStr('What is the name of your favorite city? ')
pers_rand_pass(ans1)
elif ans1 == '3' or 3:
ans1 = pyip.inputStr('Who is your favorite singer/band? ')
pers_rand_pass(ans1)
elif ans1 == '4' or 4:
print('Okay here are your randomized passwords:')
# random_pass()
This is what happens when I run it and go through the opening prompts:
Password Generator
____________________________
Welcome to your Password Generator
How many passwords do you want to generate? (enter a positive number): 5
Password length (enter a positive number): 9
Would you like a personalized or random password? Enter 0 for personalized or 1 for random: 0
You have selected to have a personalized password.
Please select a question to answer:
1. What is the name of your favorite pet?
2. What is the name of your favorite city?
3. Who is your favorite singer/band?
4. I dont want a personalized password, give me a random password
1
Traceback (most recent call last):
File "/Users/personname/Documents/OneDrive/Documents/pythonProject/password_generator.py", line 76, in <module>
personalized_pass()
File "/Users/personname/Documents/OneDrive/Documents/pythonProject/password_generator.py", line 19, in personalized_pass
ans1 = int(pyip.inputMenu(questions, prompt='Please select a question to answer: \n', numbered=True))
File "/Users/personname/Documents/OneDrive/Documents/pythonProject/venv/lib/python3.8/site-packages/pyinputplus/__init__.py", line 774, in inputMenu
result = pysv.validateChoice(
File "/Users/personname/Documents/OneDrive/Documents/pythonProject/venv/lib/python3.8/site-packages/pysimplevalidate/__init__.py", line 839, in validateChoice
_raiseValidationException(_("%r is not a valid choice.") % (_errstr(value)), excMsg)
File "/Users/personname/Documents/OneDrive/Documents/pythonProject/venv/lib/python3.8/site-packages/pysimplevalidate/__init__.py", line 222, in _raiseValidationException
raise ValidationException(str(standardExcMsg))
pysimplevalidate.ValidationException: 'What is the name of your favorite pet?' is not a valid choice.
Process finished with exit code 1
BUT when I go look up the exception, it says:
def validateChoice(
value,
choices,
blank=False,
strip=None,
allowRegexes=None,
blockRegexes=None,
numbered=False,
lettered=False,
caseSensitive=False,
excMsg=None,):
# type: (str, Sequence[Any], bool, Union[None, str, bool], Union[None, Sequence[Union[Pattern, str]]], Union[None, Sequence[Union[Pattern, str, Sequence[Union[Pattern, str]]]]], bool, bool, bool, Optional[str]) -> str
"""Raises ValidationException if value is not one of the values in
choices. Returns the selected choice.
Returns the value in choices that was selected, so it can be used inline
in an expression:
print('You chose ' + validateChoice(your_choice, ['cat', 'dog']))
Note that value itself is not returned: validateChoice('CAT', ['cat', 'dog'])
will return 'cat', not 'CAT'.
If lettered is True, lower or uppercase letters will be accepted regardless
of what caseSensitive is set to. The caseSensitive argument only matters
for matching with the text of the strings in choices.
* value (str): The value being validated.
* blank (bool): If True, a blank string will be accepted. Defaults to False.
* strip (bool, str, None): If None, whitespace is stripped from value. If a str, the characters in it are stripped from value. If False, nothing is stripped.
* allowRegexes (Sequence, None): A sequence of regex str that will explicitly pass validation, even if they aren't numbers.
* blockRegexes (Sequence, None): A sequence of regex str or (regex_str, response_str) tuples that, if matched, will explicitly fail validation.
* numbered (bool): If True, this function will also accept a string of the choice's number, i.e. '1' or '2'.
* lettered (bool): If True, this function will also accept a string of the choice's letter, i.e. 'A' or 'B' or 'a' or 'b'.
* caseSensitive (bool): If True, then the exact case of the option must be entered.
* excMsg (str): A custom message to use in the raised ValidationException.
Returns the choice selected as it appeared in choices. That is, if 'cat'
was a choice and the user entered 'CAT' while caseSensitive is False,
this function will return 'cat'.
Since Numbered is True, why is it not accepting my imputed 1 or is it the way that I tried to use the if statement for the question that gets selected?
Thanks!