-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsortNum.py
64 lines (52 loc) · 1.6 KB
/
sortNum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import random
def sortNum():
while True:
try:
val = int(input("Enter the number of values you would like to sort: "))
break
except ValueError:
print("Invalid input. Please enter a different value.")
numList = []
numCount = 0
while numCount < val:
x = random.randint(0, 100)
numList.append(x)
numCount = numCount + 1
while True:
try:
val1 = int(input("Would you like to sort in ascending or descending order? (enter 1 if ascending or enter 0 if descending): "))
break
except ValueError:
print("Invalid input. Please enter a different value.")
while val1 != 0 and val1 != 1:
val1 = int(input("Invalid input. Please re-enter value (enter 1 if yes or enter 0 if no): "))
if val1 == 1:
for x in range(val + 1):
for x in range(val - 1):
if numList[x] > numList[x + 1]:
temp = numList[x + 1]
numList[x + 1] = numList[x]
numList[x] = temp
if val1 == 0:
for x in range(val + 1):
for x in range(val - 1):
if numList[x] < numList[x + 1]:
temp = numList[x + 1]
numList[x + 1] = numList[x]
numList[x] = temp
print("Sorted values:")
for x in range(val):
print(numList[x])
while True:
try:
val1 = int(input("Would you like to sort a different set of values? (enter 1 is yes or enter 0 if no): "))
break
except ValueError:
print("Invalid input. Please enter a different value.")
while val1 != 0 and val1 != 1:
val1 = int(input("Invalid input. Please re-enter value (enter 1 if yes or enter 0 if no): "))
if val1 == 1:
sortNum()
if val1 == 0:
quit()
sortNum()