python作业代码没有缩进严重吗_有没有一种方法可以将Python代码中的缩进转换为大括号?...

1586010002-jmsa.png

I am a totally blind programmer who would like to learn Python. Unfortunately the fact that code blocks are represented with different levels of indentation is a major stumbling block. I was wondering if there were any tools available that would allow me to write code using braces or some other code block delimiter and then convert that format into a properly indented representation that the Python interpreter could use?

解决方案

There's a solution to your problem that is distributed with python itself. pindent.py, it's located in the Tools\Scripts directory in a windows install (my path to it is C:\Python25\Tools\Scripts), it looks like you'd have to grab it from svn.python.org if you are running on Linux or OSX.

It adds comments when blocks are closed, or can properly indent code if comments are put in. Here's an example of the code outputted by pindent with the command:

pindent.py -c myfile.py

def foobar(a, b):

if a == b:

a = a+1

elif a < b:

b = b-1

if b > a: a = a-1

# end if

else:

print 'oops!'

# end if

# end def foobar

Where the original myfile.py was:

def foobar(a, b):

if a == b:

a = a+1

elif a < b:

b = b-1

if b > a: a = a-1

else:

print 'oops!'

You can also use pindent.py -r to insert the correct indentation based on comments (read the header of pindent.py for details), this should allow you to code in python without worrying about indentation.

For example, running pindent.py -r myfile.py will convert the following code in myfile.py into the same properly indented (and also commented) code as produced by the pindent.py -c example above:

def foobar(a, b):

if a == b:

a = a+1

elif a < b:

b = b-1

if b > a: a = a-1

# end if

else:

print 'oops!'

# end if

# end def foobar

I'd be interested to learn what solution you end up using, if you require any further assistance, please comment on this post and I'll try to help.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值