倒序输出

在codecademy上面的题目:
Define a function called reverse that takes a string text and returns that string in reverse.

For example:reverse("abcd") should return "dcba".
  1. You may not use reversedor [:: -1]to help you with this.
  2. You may get a string containing special characters(for example,!,@,or #)


    1
    def reverse_try(text): 2 i = len(text) 3 new = "" 4 while i >= 1: #while i >= 0 ,in this case,it'll print !fes23! 5 print text[i - 1] 6 new += text[i - 1] 7 i -= 1 8 return new 9 10 print reverse_try("32sef!")

运行结果:

如果使用while >= 0,将会输出:

之后又尝试更换输入方式:

1 def reverse_2(text):
2     i = len(text)
3     while i >= 1:
4         print text[i - 1],
5         i -= 1
6         
7 print reverse_2("32sef!")

结果如下:

思考:

  1. Python字符串输出和单次输出如上图所示,格式上面有区别;
  2. [index],index正负数表示开始的方向不一样。

第一次尝试写这样的代码总结,希望自己写的越来越好。

  • 多读大神的优秀的代码,然后对比,看看比自己写的代码好在哪里。
  • 不断的写,不断的更正,相信自己可以写出好的文章出来
  • 虚心接受建议,善于学习和总结

posted on 2014-05-21 10:24  wingo0527  阅读(113)  评论(0)    收藏  举报