Advertisement
Guest User

Untitled

a guest
Aug 12th, 2022
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.87 KB | None | 0 0
  1. def add_candidate():
  2.     #!!candidate data variables defined by user input eg. soft coded candidate data
  3.     #name = input("What is the candidate's name?: ")
  4.     #birthday = str(input('When was the candidate born? (format: D.M.YYYY): '))
  5.     #height = str(input('How tall is the candidate?: '))
  6.     #weight = str(input('How much does the candidate weigh in kilograms?: '))
  7.     #car = str(input('Does the candidate have a car?: '))
  8.     #languages = str(input('What (programming) language/s does the candidate know? (please separate by commas, and place languages in quotation marks""): '))
  9.     #languages_formatted = str("{languages}")
  10.  
  11.     #if car == 'Yes' or car == 'yes':
  12.     #    car = True
  13.     #elif car == 'No' or car == 'no':
  14.     #    car == False
  15.  
  16.     filename = 'maze.json'
  17.     listObj = []
  18.    
  19.     # Check if file exists
  20.     if path.isfile(filename) is False:
  21.         raise Exception("File not found")
  22.    
  23.     # Read JSON file
  24.     with open(filename) as fp:
  25.         listObj = json.load(fp)
  26.    
  27.     # Verify existing list
  28.     print(listObj)
  29.  
  30.     print(type(listObj))
  31.    
  32.     #!! JSON entry to be used with input based candidate data
  33.     #listObj.append({
  34.     #"name": "{name}",
  35.     #"birthday": "{birthday}",
  36.     #"height": height,
  37.     #"weight": weight,
  38.     #"car": car,
  39.     #"languages": ["{languages_formatted}"]
  40.     #})
  41.    
  42.     #!! hard coded candidate data
  43.     listObj.append({
  44.     "name": "John Doe",
  45.     "birthday": "21.02.2005",
  46.     "height": 180,
  47.     "weight": 69,
  48.     "car": False,
  49.     "languages": ["Python", "Scratch"] #Scratch OMEGALUL
  50.     })
  51.    
  52.     # Verify updated list
  53.     print(listObj)
  54.    
  55.     with open(filename, 'w') as json_file:
  56.         json.dump(listObj, json_file,
  57.                             indent=4,  
  58.                             separators=(',',': '))
  59.    
  60.     print('Successfully appended to the JSON file')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement