Advertisement
furas

Python - sorting example

Aug 7th, 2016
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.25 KB | None | 0 0
  1. ### create list with 3 elements
  2. ### but you could create empty list `num = []` and later use `num.append(value)`
  3.  
  4. #num = [x for x in range(3)]
  5. num = range(3)
  6.  
  7. ### default value to get into `while`
  8.  
  9. answer = 'y'
  10.  
  11. ### repeat till someone answers something different then `y`
  12. #while(answer == 'y'):  # doesn't need ()
  13. while answer == 'y':
  14.  
  15.     ### ask for 3 number
  16.     for i in range(3):
  17.         # ask for number, convert answer from text to integer, put number on list
  18.         num[i] = int(raw_input("Enter any value for num["+ str(i) +"] : "))
  19.  
  20.         # if you use empty list you could use `num.append( int(raw_input(...) )`
  21.  
  22.  
  23.     for z in range(3):
  24.         for x in range(3):
  25.         #if(x <= 1):  # doesn't need ()
  26.         if x <= 1:
  27.             #if(num[x+1] > num[x]):  # doesn't need ()
  28.             if num[x+1] > num[x]:
  29.                 ### replace numbers
  30.                 #ix = num[x]
  31.                 #num[x] = num[x+1]
  32.                 #num[x+1] = ix
  33.                 num[x], num[x+1] = num[x+1], num[x] # pythonic method
  34.  
  35.     print()
  36.  
  37.     ### display list elements
  38.     #for u in range(3):
  39.     #   print(num[u])
  40.  
  41.     for element in num: # pythonic method
  42.         print(element)
  43.  
  44.     ### ask question and remember answer
  45.     answer = raw_input("Try again [y/n] : ")
  46.  
  47.     ### automaticaly return to `while` with new value in `answer`
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement