问题-python动态生成

一马平川 发布于 2009/10/29 15:51
阅读 633
收藏 0

大家帮我看看这个程序哪里有错?我运行不出来。。,谢谢啊!!!

#!/user/bin/env python
import os
dashes='\n'+'-'*50
exec_dict={
    'f':"""
    for %s in %s:
        print %s
    """,
    's':"""
    %s=0
    %s=%s
    while %s<len(%s):
        print %s[%s]
        %s=%s+1
    """,
    'n':"""
    %s=%d
    while %s<%d:
        print %s
        %s=%s+%d
    """
    }
def main():
    ltype=raw_input('loop type?(for/while):')
    dtype=raw_input('data type?(number/seq)')
    if dtype=='n':
        start=input('starting value?')
        stop=input('ending value (non-inclusive)?')
        step=input('stepping value?')
        seq=str(range(start,stop,step))
        
    else:
        seq=raw_input('enter sequence:')
        var=raw_input('iterative variable name? ')
        if ltype=='f':
            exec_str=exec_dict['f'] %(var,seq,var)
        elif ltype=='w':
           if dtype=='s':
                  svar=raw_input('enter sequence name?')
                  exec_str=exec_dict['s']%\
                  (var,svar,seq,var,svar,svar,var,var,var)
           elif dtype=='n':
                  exec_str=exec_dict['n']%\
                  (var,start,var,stop,var,var,var,step)
    print dashes
    print 'your custom-generated code:'+dashes
    print exec_str+dashes
    print 'test execution of the code:'+dashes
    exec exec_str
    print dashes
if __name__=='__main__':
    main()

加载中
0
滔滔下载站
滔滔下载站

这个程序写的提示都看不清楚?

  ltype=raw_input('loop type?(for/while):')

    dtype=raw_input('data type?(number/seq)')

look type?

data type?

都是神马?

返回顶部
顶部