翻译伪代码回文

时间:2016-03-13 20:10:27

标签: python pseudocode

我有这个需要翻译的伪代码:

Prompt the user to enter a string and call it s.
Let l be the length of string
For i from 0 upto l-1:
    print s[0:i]
For i from 0 upto l-1:
    print s[i:l]
Print a closing statement

这是我的翻译:

 def main():

        s=(input("Please enter a string: "))
        L=len(s)

        for i in [0,L-1]:
            print (s[0:i])

        for i in [0,L-1]:
            print(s[i:L])

        print("This program is complete!")

    main()

但是,代码无法正确打印。有人可以帮我找到我的错误吗?谢谢。

1 个答案:

答案 0 :(得分:1)

您说for i in [0,L-1],但[0,L-1]是包含两个元素的列表:0L-1。您想要的是range(0, L)range(L)

def main():

    s=(input("Please enter a string: "))
    L=len(s)

    for i in range(L):
        print (s[:i])

    for i in range(L):
        print(s[i:L])

    print("This program is complete!")

main()