Advertisement
Mars83

6-1 + 6-2

Oct 9th, 2011
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.66 KB | None | 0 0
  1. #! /usr/bin/env python3.2
  2. # -*- coding: utf-8 -*-
  3.  
  4. # main.py
  5. """ Task: Exercise 6.1
  6.    Write a while loop that starts at the last character in the string and
  7.    works its way backwards to the first character in the string, printing each
  8.    letter on a separate line, except backwards.
  9.    
  10.    fruit = 'banana'
  11.    index = 0
  12.    while index < len(fruit):
  13.        letter = fruit[index]
  14.        print(letter)
  15.        index = index + 1
  16. """
  17.  
  18. ''' Main '''
  19. fruit = 'banana'
  20. index = len(fruit) - 1
  21. while index >= 0:
  22.     letter = fruit[index]
  23.     print(letter)
  24.     index -= 1
  25.  
  26. fruit = 'B' + fruit[1:]
  27.  
  28. print(fruit[:]) # Exercise 6.2, output: 'Banana'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement