Popcorn Hack #1

Create a Dictionary of Fruits

Please create a dictionary containing 3 different type of names of fruits as variables, and assign them to numbers 1, 2, and 3. Please print out the output of accessing one value through its corresponding key. Make sure to do it for both javascript and python

%%js


// Create a dictionary (object) in JavaScript
var myDictionary = {
    1: "fruit",
    2: "fruit",
    3: "fruit"
};

// Accessing a value
console.log("Fruit with key 2:", myDictionary[2]); // Output: fruit
<IPython.core.display.Javascript object>

#Python

# Create a dictionary in Python
my_dictionary = {
    1: "fruit",
    2: "fruit",
    3: "fruit"
}

# Accessing a value
print("Fruit with a key:", my_dictionary[2])  # Output: fruit
Fruit with a key: fruit

Popcorn Hack 2

Popcorn Hack #3

Temperature Converter

In this hack, you will create a program that converts temperatures between Celsius and Fahrenheit. The program will ask the user for a temperature and the desired conversion direction (Celsius to Fahrenheit or Fahrenheit to Celsius).

Objective:

  • Create a temperature converter in both JavaScript and Python.
  • Allow users to input a temperature and choose the conversion type.
%%js

// Temperature Converter in JavaScript
let temperature = parseFloat(prompt("Enter the temperature:"));
let conversionType = prompt("Convert to (C)elsius or (F)ahrenheit?").toUpperCase();

if (conversionType === "C") {
    // Convert Fahrenheit to Celsius
    let celsius = (temperature - 32) * (5 / 9);
    console.log(`${temperature}°F is equal to ${celsius.toFixed(2)}°C`);
} else if (conversionType === "F") {
    // Convert Celsius to Fahrenheit
    let fahrenheit = (temperature * (9 / 5)) + 32;
    console.log(`${temperature}°C is equal to ${fahrenheit.toFixed(2)}°F`);
} else {
    console.log("Invalid conversion type entered.");
}
<IPython.core.display.Javascript object>
#Python

# Temperature Converter Template

# Function to convert temperatures
def temperature_converter():
    try:
        # Prompt the user for the temperature
        temperature = float(input("Enter the temperature: "))
        
        # Ask the user for the conversion type
        conversion_type = input("Convert to Celsius or Fahrenheit? ").strip().upper()

        if conversion_type == "C":
            pass
            # TODO: Convert Fahrenheit to Celsius
            # celsius = (temperature - 32) * (5 / 9)
            # print(f"{temperature}°F is equal to {celsius:.2f}°C")

        elif conversion_type == "F":
            pass
            # TODO: Convert Celsius to Fahrenheit
            # fahrenheit = (temperature * (9 / 5)) + 32
            # print(f"{temperature}°C is equal to {fahrenheit:.2f}°F")

        else:
            print("Invalid conversion type entered. Please enter 'C' or 'F'.")

    except ValueError:
        print("Invalid input. Please enter a numeric temperature value.")

# Call the temperature converter function
temperature_converter()


Invalid conversion type entered. Please enter 'C' or 'F'.