Advertisement
Guest User

EOY programming test Y10- Vet Scenario

a guest
Jun 25th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.12 KB | None | 0 0
  1. def displayName():
  2.     """ This is a procedure which will ask the user for the name of their
  3.        vet surgery, and then welcome them using the name they  have just
  4.        input into the program.
  5.    """
  6.     surgery = input("What is the name of the Vet Surgery? > ")
  7.     print("Welcome to the {0} Veterinary Surgery.".format(surgery))
  8.     menuProgram() #Takes user back to subprogram selection.
  9.  
  10. #---------------------------------------------------------------------------------------
  11.  
  12. def checkUsernamePassword():
  13.     """ This is a procedure which will ask the user for their username
  14.        and password, and if they get a valid username and password
  15.        they will get the message 'valid username and password', but if
  16.        either the username or password are incorrect, the program will
  17.        display 'invalid username and password'.
  18.    """
  19.     username = input("Please enter your username > ")
  20.     password = input("Please enter your password > ")
  21.  
  22.     while username != "admin" or password != "ABC123*":
  23.         print("Invalid username or password.")
  24.         username = input("Please enter your username > ")
  25.         password = input("Please enter your password > ")
  26.     print ("Valid username and password.")
  27.  
  28. #---------------------------------------------------------------------------------------
  29.  
  30. def totalCost(mylist):
  31.     """ This is a function which will calculate the total of a given
  32.        list in a 'for' loop, and return the value to the main program.
  33.    """
  34.     cost = 0
  35.     for element in mylist:
  36.         cost = cost + element
  37.     return cost
  38.  
  39. #---------------------------------------------------------------------------------------
  40.  
  41. def writeToFile():
  42.     """ This is a function that allows the user to enter the firstname
  43.        and surname of the owner, the name of the pet and the type of animal-
  44.        this information will then be written to file.
  45.    """
  46.     answer = "Y" #Sets the initial guard variable to "Y" so that the program will enter
  47.                  #the while loop at least once.
  48.     while answer == "Y":
  49.         animalfile = open("animals.txt","a") #Opens the file 'animals.txt' (or will create
  50.                                              #a file of the same name if it doesn't already
  51.                                              #exist) in append mode, so that data isn't overwritten
  52.                                              #every time the program runs.
  53.        
  54.         firstname = input("Enter first name of owner > ") #Asking for the data to be appended onto file
  55.         surname = input("Enter surname of owner > ")      # |
  56.         petname = input("Enter pet name > ")              # |
  57.         animaltype = input("Enter type of animal > ")     # v
  58.         record = "{0} {1} {2} {3} \n".format(firstname, surname, petname, animaltype)
  59.         #The variable 'record' is comprised of the 4 seperate variables created above- they are compiled
  60.         #into the new variable 'record' so it only requires one 'fileName.write()' command as opposed
  61.         #to four seperate ones.
  62.         animalfile.write(record) #Writes the contents of the variable 'record onto the file'
  63.        
  64.         answer = input("Do you want to enter another customer Y/N ? > ") #Asks the user whether they would
  65.         validList = ["Y","N"] #A list of valid answers                   #like to continue or not
  66.         while answer not in validList: #Checks whether the users answer is in the valid list or not.
  67.                                        #User will enter this while loop if it is not "Y" or "N"
  68.             print("Invalid selection. Please choose again.")
  69.             answer = input("Do you want to enter another customer Y/N ? > ")
  70.        
  71.     animalfile.close() #Closes the file so that other programs may access it.
  72.     menuProgram() #Takes user back to subprogram selection.
  73.  
  74. #---------------------------------------------------------------------------------------
  75.  
  76. """ MAIN PROGRAM- Computer System for the Red Bank Veterinary Surgery
  77. """
  78. print("LOGIN TO SYSTEM:") #Initial login- must do this before you can access other programs
  79. checkUsernamePassword()
  80.  
  81. def menuProgram():
  82.     """ This is the menu which enables you to choose which sub-program you would like to run.
  83.    """
  84.     print ("MENU:\n\n PRESS:\n\t1 for Welcome Message Program\n\t2 for Fluffy's Total Cost Program")
  85.     print("\t3 for Patient Input Program\n\t0 to quit the program.")
  86.     choice = input(" > ")
  87.  
  88.     if choice == "1":
  89.         displayName()                                               # First Sub-Program
  90.  
  91.     elif choice == "2":
  92.         fluffy = [10,5,8,20,8]                                      #Third Sub-
  93.         totalcost = totalCost(fluffy)                               #Program
  94.         print("Total cost for fluffy is £{0}.".format(totalcost))
  95.         menuProgram()
  96.  
  97.     elif choice == "3":
  98.         writeToFile()                                               #Fourth Sub-
  99.                                                                     #Program
  100.     elif choice == "0":
  101.         print("Goodbye.")
  102.     else:
  103.         print("I didn't quite get that.")
  104.         menuProgram()
  105.  
  106. menuProgram()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement