Skip to the content.

3.2 Homework

hw for 3.2 lesson

3.2 Homework

let facts = true;
console.log("Magic 8 Ball, do I get an A on this assignment");
console.log("[8] Magic 8 Ball is thinking ...");
console.log("[8] The ball has decided, that is " + facts);

/*
Magic 8 Ball, do I get an A on this assignment 
[8] Magic 8 Ball is thinking ...
[8] The ball has decided, that is true
*/

facts = True
print("Magic 8 Ball, do I get an A on this assignment")
print("[8] Magic 8 Ball is thinking ...")
print("[8] The ball has decided, that is " + str(facts))

'''
Magic 8 Ball, do I get an A on this assignment
[8] Magic 8 Ball is thinking ...
[8] The ball has decided, that is True
'''

import json 

# dict
volkswagen_beetle = {
    "name": "2019 Volkswagen Beetle",
    "performance": {
        "engine": "Intercooled Turbo Regular Unleaded I-4",
        "horsepower": 170,
    },
    "pricing": {
        "original_price": "$29,995",
        "rarity": "random car"
    }
}

90
jsonstring = json.dumps(volkswagen_beetle) 

print(jsonstring)

let facts = true;
console.log("Magic 8 Ball, do I get an A on this assignment");
console.log("[8] Magic 8 Ball is thinking ...");
console.log("[8] The ball has decided, that is " + facts);

/*
Magic 8 Ball, do I get an A on this assignment 
[8] Magic 8 Ball is thinking ...
[8] The ball has decided, that is true
*/


const volkswagenBeetle = {
    name: "2019 Volkswagen Beetle",
    performance: {
        engine: "Intercooled Turbo Regular Unleaded I-4",
        horsepower: 170
    },
    pricing: {
        original_price: "$29,995",
        rarity: "random car"
    }
};

const jsonString = JSON.stringify(volkswagenBeetle);

console.log(jsonString);

// Fetch the JSON data
fetch('https://jsonplaceholder.typicode.com/todos/1')
  .then(response => response.json())
  .then(json => {
    // Original JSON
    console.log('Original JSON:', json);

    // Modify the JSON data
    json.title = "Updated title";          // String
    json.userId = 99;                      // Number
    json.completed = true;                 // Boolean
    json.tags = ["urgent", "homework"];    // Array (adding new key 'tags')
    json.details = {                       // Object (adding new key 'details')
      description: "hello",
    };

    // Print the modified JSON
    console.log('Modified JSON:', json);
  });

import requests
import json

# Fetch the JSON data
response = requests.get('https://jsonplaceholder.typicode.com/todos/1')
json_data = response.json()

# Original JSON
print("Original JSON:", json_data)

# Modify the JSON data
json_data['title'] = "Updated title"       # String
json_data['userId'] = 99                   # Number
json_data['completed'] = True              # Boolean
json_data['tags'] = ["urgent", "homework"] # Array (adding new key 'tags')
json_data['details'] = {                   # Object (adding new key 'details')
    "description": "hello",
}

# Print the modified JSON
print("Modified JSON:", json.dumps(json_data, indent=4))