Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. import datetime
  2. timeDate = datetime.datetime.now()
  3.  
  4. def WriteData():
  5. userName = input("Please enter your name.\n")
  6. print("\nThis name has " + str(len(userName)) + " letters in it.\n" )
  7. nameSplit = userName.split(' ')
  8. nameCount = len(nameSplit)
  9. print("The names you have inputted includes " + str(nameCount) + " names. \n")
  10. f = open("usersname.txt", "a")
  11. f.write("Name entered:" + "," + userName + "\n" + "Time and Date entered:" + "," + str(timeDate) + "\n" )
  12. f.close()
  13. 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")
  14. if readFile == "1":
  15. f = open("usersname.txt", "r")
  16. for line in f:
  17. data = line.split(",")
  18. # Above I split the data by commas, and stores this in a variable called data.
  19. print(data)
  20.  
  21. def WriteDataTwo(numberList):
  22. f = open("NumberLists.txt", "a")
  23. f.write("List of sorted inputted numbers:" + str(numberList) + "\n" + "Time and Date entered:" + "," + str(timeDate) + "\n" )
  24. f.close()
  25. 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")
  26. if readListFile == "1":
  27. f = open("NumberLists.txt", "r")
  28. # Above I open the text file in a read only mode
  29. for line in f:
  30. data = line.split(",")
  31. print(data)
  32.  
  33. def AppendList():
  34. numberList = []
  35. while True:
  36. newNumbers = input("Please enter a number to add to the list. To begin sorting your list, type 'sort'.\n ")
  37. if newNumbers == "sort":
  38. break
  39. numberList.append(int(newNumbers))
  40. print("This is how the list currently looks: " + str(numberList) + ",")
  41. return numberList
  42.  
  43. def BubbleSort():
  44. numberList = AppendList()
  45. swapped = True
  46. while swapped:
  47. swapped = False
  48. for numberPosition in range(len(numberList)-1):
  49. print("Checking position", numberPosition)
  50. if numberList[numberPosition] > numberList[numberPosition+1]:
  51. firstNumber = numberList[numberPosition]
  52. secondNumber = numberList[numberPosition+1]
  53. print("Swapped numbers", firstNumber, "and", secondNumber,)
  54. numberList[numberPosition] = secondNumber
  55. numberList[numberPosition+1] = firstNumber
  56. print(numberList)
  57. swapped = True
  58. if not swapped:
  59. print("No more swaps required.")
  60. print(numberList)
  61. else:
  62. print("More swaps required.")
  63. WriteDataTwo(numberList)
  64.  
  65.  
  66. def UserMenu():
  67. while True:
  68. 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")
  69. if menuChoice == "1":
  70. WriteData()
  71. elif menuChoice == "2":
  72. BubbleSort()
  73. elif menuChoice == "3":
  74. exit()
  75. else:
  76. print("That input is not a recognized answer. Please pick one of the three options by typing the number 1, 2 or 3. \n")
  77. UserMenu()
  78.  
  79. UserMenu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement