Solution to python alignment error

When running, sometimes there will be a syntax error: IndentationError: unexpected indent

You can use the following methods to solve:

First display the spaces, where the spaces are replaced by dots

Modify the tab to represent 4 positions

Then the format is aligned.

Example extension:

How to fix text alignment

Hello everyone, I am a newbie in python, and I encountered a problem in an exercise problem.

The requirement of the title is to print out and align the list.

Input data:

tableData =[['apples','oranges','cherries','banana'],['Alice','Bob','Carol','David'],['dogs','cats','moose','goose']]

Required output data (right-aligned first row, left-aligned others):

 apples Alice dogs
 oranges Bob  cats
cherries Carol moose
 banana David goose

Below is my code

""" Below is the code body"""
tableData =[['apples','oranges','cherries','banana'],['Alice','Bob','Carol','David'],['dogs','cats','moose','goose']]

def printTable(tableData):
 # The following is to find the length of the longest string of each inner list
 colWidths =[0]*len(tableData)for i inrange(len(colWidths)):
 colWidths[i]=len(sorted(tableData[i], key=(lambda x:len(x)))[-1])for x inrange(len(tableData[0])):for y inrange(len(tableData)):print(tableData[y][x].rjust(colWidths[y]), end=' ')print('')  #Wrap

printTable(tableData)

The output is (all right aligned):

 apples Alice dogs 
 oranges  Bob cats 
cherries Carol moose 

Recommended Posts

Solution to python alignment error
01. Introduction to Python
Introduction to Python
Centos 6.4 python 2.6 upgrade to 2.7
Centos 6.4 python 2.6 upgrade to 2.7
Python error handling method
Centos default python2.6 upgrade to
CentOS upgrade python2 to pyth
Python code to find bugs (2)
Python code to find bugs(7)
How to comment python code
Python runtime exception management solution
Python code to find bugs(4)
Python code to find bugs (3)
Python code to find bugs(9)
How to learn python quickly
How to uninstall python plugin
Introduction to Python related modules
Python code to find bugs(6)
Python code to find bugs (1)
Python code to find bugs(8)
3 ways to encrypt Python files
How to understand python objects
Python code to find bugs(5)
How to use python tuples
Python right alignment example method
Python| function using recursion to solve
Method steps to increase python font
python how to view webpage code
How to use python thread pool
Use python to query Oracle database
Use C++ to write Python3 extensions
How to write python configuration file
Python novice learns to raise usage
Use python to achieve stepwise regression
How to wrap in python code
How to save the python program
Python error handling assert detailed explanation
Python implements 126 mailboxes to send mail
Using Python to implement multiple clipboards
How to omit parentheses in Python
How to install Python 3.8 on CentOS 8
Python example to calculate IV value
How to install Python 3.8 on Ubuntu 18.04
Python devel installation failure problem solution
How to write classes in python
How to filter numbers in python
Centos6.7 comes with python upgrade to
How to read Excel in Python
How to install Python on CentOS 8
How to solve python dict garbled
How to view errors in python
Python from entry to proficiency (2): Introduction to Python
Steps to enter python through cmd
How to write return in python
How to view the python module
How to understand variables in Python
How to clear variables in python
How to understand python object-oriented programming
Python uses pdfkit to generate pdf [python]
CentOS7 repairs python to save yum