Python program to specify Number of String inputs and Bubble Sort them in Descending order

Python program to specify Number of String inputs and Bubble Sort them in Descending order

There are many algorithms around Sorting. Bubble Sort is one of the Sorting algorithms. This Python code is Bubble Sort module accepts an array of Strings and array size and sorts the Array in Descending order when called. The call function that represents the module is “main()”

The main() function allows the user to enter any specified Number of names into a String array, Sort the array in Descending (alphabetical) order and display its contents. Below is the code:

#!/usr/bin/python3

def main():

# Declare variables
nameCount = 0 
name = ""
stringsArray = []

# Ask user for Names - which must be letters or the program throws an exception
upperLimit = int(input("How many names do you want enter to Sort them in Descending order (e.g., 20)?: \n"))

print("Please enter {} names to sort them in Descending order!\n\n".format(upperLimit))


while nameCount < upperLimit:
some
name = input("Enter name: ")

# Check if string contains anything other than letters and throw an exception if its the case
if name.isalpha():

stringsArray.append(name)
nameCount += 1


# Simple array sort using the sort() function
stringsArray.sort(reverse = True)

else:
print("\nYou entered characters that are not letters!\nPlease enter characters in A-Z!")

print("\n\nYou entered {} names and this is the list in a Descending order:\n ".format(upperLimit) + str(stringsArray) + "!")

main ()


NOTE:// On most IDEs, indentation is a big deal because it dictates where what function or loop starts/stops. For this reason, always confirm that the INDENTS are all good before running the program. It is of course better to use an IDE that automatically indents the code for you.

 

Python program to specify Number of String inputs and Bubble Sort them in Descending order
Python | thetqweb