When substituting the list for %-15s in python,

Asked 2 years ago, Updated 2 years ago, 65 views

def manual() :
    menuList = [1. Determining the root of the quadratic equation], "2. Finding the internal branch", "3. Finding the external branch",
                "4. Find the center of gravity", "5. Read the instructions", "9. End the program"]

    print("==============================================")
    print("==========Troubleshooting Help ===========")
    print("==============================================")
    for menu in menuList : 
        print("========== %-15s==========" % menu)
    print("==============================================")

When manual() is executed

==============================================
========== Troubleshooting Help ===========
==============================================
========== 1. Determination of the root of the quadratic equation ==========
========== 2. Finding an internal branch ==========
========== 3. Finding an external branch ==========
========== 4. Find the center of gravity ==========
========== 5. View user description ==========
========== 9. Exit the program ==========
==============================================

Is there a way to make it look neat when it's not aligned like this?

python list

2022-09-22 18:37

1 Answers

The question you are asking is whitespace padding of full-width characters.

When I looked up how Python could handle this problem, Well, it seems to be a little annoying. Well, I don't know if that menu is a value that has to be calculated and organized because it has to be used by users. In this situation, I'd just do this.

def manual() :
    # Introduced dictionaries to have minimal systematicity.
    menuList = {
        '1': {
            'name' : 'Determination of root of quadratic equation',
            'label': '1. Determining the root of a quadratic equation'
        },
        '2': {
            'name' : 'Saving my branch',
            'label': '2. Finding an internal branch' # This blank is important
        },
        '3': {
            'name' : 'Saving an extraterrestrial branch',
            'label': '3. Finding an extraterrestrial branch'
        },
        '4': {
            'name' : 'Getting the center of gravity',
            'label': '4. Finding the center of gravity'
        },
        '5': {
            'name' : 'View user description',
            'label': '5. View instruction'
        },
        '9': {
            'name' : 'End of program',
            'label': '9. End of program'
        }
    }

    print("==================================================")
    print("=============Troubleshooting Help =============")
    print("==================================================")
    for menuCode, menuInfo in menuList.items() :
        print("=========== " + menuInfo['label'] + " ===========")
    print("==================================================")

manual()

Coding sewage VS coding adherence joke seems to come from this context. For me, it's ㅈ rice.


2022-09-22 18:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.