Advertisement
matisarnowski

script_sort.py

Mar 18th, 2023
803
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | None | 0 0
  1. #! /usr/bin/python3
  2.  
  3. import random
  4. import pprint
  5. while True:
  6.     try:
  7.         n = input("Wprowadż liczbę naturalną, mówiącą ile losowych liczb chcesz wygenerować, będą to liczby naturalne od 0 do 100. Aby je następnie posortować: ")
  8.         n = int(n)
  9.         if n < 2:
  10.             print("Wprowadź liczbę naturalną większą od dwóch.")
  11.             continue
  12.     except Exception as e:
  13.         print(e)
  14.         continue
  15.     break
  16.  
  17. tab = []
  18.  
  19. for i in range(0, n):
  20.     tab.append(random.randint(0, 101))
  21.  
  22. for j in range(0, n):
  23.     for k in range(j+1, n):
  24.         if tab[j] > tab[k]:
  25.             tmp = tab[j]
  26.             tab[j] = tab[k]
  27.             tab[k] = tmp
  28. pprint.pprint(tab)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement