Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import datetime
- timeDate = datetime.datetime.now()
- def WriteData():
- userName = input("Please enter your name.\n")
- print("\nThis name has " + str(len(userName)) + " letters in it.\n" )
- nameSplit = userName.split(' ')
- nameCount = len(nameSplit)
- print("The names you have inputted includes " + str(nameCount) + " names. \n")
- f = open("usersname.txt", "a")
- f.write("Name entered:" + "," + userName + "\n" + "Time and Date entered:" + "," + str(timeDate) + "\n" )
- f.close()
- readFile = input("The program has dded your name to a text file. Would you like to read the text file?\n1.Yes\n2.No\n")
- if readFile == "1":
- f = open("usersname.txt", "r")
- for line in f:
- data = line.split(",")
- # Above I split the data by commas, and stores this in a variable called data.
- print(data)
- def WriteDataTwo(numberList):
- f = open("NumberLists.txt", "a")
- f.write("List of sorted inputted numbers:" + str(numberList) + "\n" + "Time and Date entered:" + "," + str(timeDate) + "\n" )
- f.close()
- readListFile = input("List of numbers appended to the text file 'NumberLists.txt'\nWould you like to read the text file?\n1.Yes\n2.No\n")
- if readListFile == "1":
- f = open("NumberLists.txt", "r")
- # Above I open the text file in a read only mode
- for line in f:
- data = line.split(",")
- print(data)
- def AppendList():
- numberList = []
- while True:
- newNumbers = input("Please enter a number to add to the list. To begin sorting your list, type 'sort'.\n ")
- if newNumbers == "sort":
- break
- numberList.append(int(newNumbers))
- print("This is how the list currently looks: " + str(numberList) + ",")
- return numberList
- def BubbleSort():
- numberList = AppendList()
- swapped = True
- while swapped:
- swapped = False
- for numberPosition in range(len(numberList)-1):
- print("Checking position", numberPosition)
- if numberList[numberPosition] > numberList[numberPosition+1]:
- firstNumber = numberList[numberPosition]
- secondNumber = numberList[numberPosition+1]
- print("Swapped numbers", firstNumber, "and", secondNumber,)
- numberList[numberPosition] = secondNumber
- numberList[numberPosition+1] = firstNumber
- print(numberList)
- swapped = True
- if not swapped:
- print("No more swaps required.")
- print(numberList)
- else:
- print("More swaps required.")
- WriteDataTwo(numberList)
- def UserMenu():
- while True:
- menuChoice = input("Welcome. What would you like the program to do?\n1.Enter your name and append it to a text file\n2.Enter a list of numbers and sort them\n3.Exit the program\n")
- if menuChoice == "1":
- WriteData()
- elif menuChoice == "2":
- BubbleSort()
- elif menuChoice == "3":
- exit()
- else:
- print("That input is not a recognized answer. Please pick one of the three options by typing the number 1, 2 or 3. \n")
- UserMenu()
- UserMenu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement