trying to simplify some boolean statements in python

Question:

I’m "newish" to python programming. I’m trying my best to make my code look nice and function well. I’m using Pycharm as my IDE. I’m doing something for myself. I play tabletop RPG’s and I’m attempting to create a character creator for a game I play. I have everything working well, but Pycharm is telling me that "Expression can be simplified" and "PEP 8: E712 comparison to True should be ‘if cond is not True:’ or ‘if not cond:’"

Here is the code in question:

fname = False
while fname != True:
    new_character.firstName = input('What would you like your first name to be?n').capitalize()
    if 1 >= len(new_character.firstName) or len(new_character.firstName) > 20:
        print('Name does not meet length requirements. Please try again.')
    if new_character.firstName.isalpha() != True:
        print('Please do not use numbers or special characters in your name. Please try again.')
    if (1 < len(new_character.firstName) < 20) and (new_character.firstName.isalpha() == True):
        fname = True

Pycharm is telling me that my "while fname != True:" is the part that can be simplified as well as the "if new_character.firstName.isalpha() != True:".

I’ve tried googling a solution for what I’m doing, but most of them are for something kinda like what I’m asking, but never with the != True portion. I’ve even reached out to one of my friends that’s a python programmer, but I haven’t heard back yet.

Again, I want to state that as it is now, the code works correctly the way it is written, I’m just wanting to understand if there is a way to make the code look cleaner/neater or do the same function and be simplified somehow.

Any pointers on how to potentially simplify those lines of code and maintain the functionality would be greatly appreciated.

Asked By: Billy Blanks

||

Answers:

Here’s one way you could rewrite this code to make it easier to read, and more efficient:

# Loop until the user provides a good input
while True:
    # Set a temp variable, don't constantly reassign to the new_character.firstName attribute
    name = input('What would you like your first name to be?n').capitalize()

    # If the name isn't between 2 and 20 characters, start the loop over at the beginning
    if not (1 < len(name) <= 20):
        print('Name does not meet length requirements. Please try again.')
        continue

    # If the name contains anything other than letters, start the loop over at the beginning
    if not name.isalpha():
        print('Please do not use numbers or special characters in your name. Please try again.')
        continue

    # You can only reach this break if the name "passed" the two checks above
    break

# Finally, assign the character name
new_character.firstName = name

One thing you could do to simplify further is to check both conditions at the same time, and print a more helpful error message that re-states the requirements explicitly:

NAME_ERROR_MESSAGE = """
Invalid name '{name}'. Your character's name
must be between 2 and 20 characters long, and
contain only letters. Please try again.
"""

while True:
    name = input('What would you like your first name to be?n').capitalize()

    if (1 < len(name) <= 20) and name.isalpha():
        new_character.firstName = name
        break

    print(NAME_ERROR_MESSAGE.format(name=name)
Answered By: ddejohn
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.