Big Idea 3 Section 1
Popcorn hacks and Homework for Section 1 of Big Idea 3
Big Idea 3 Section 1
Popcorn Hack #1
%%js
var fruitdictionary = {
1: "Apples 🍎",
2: "Oranges 🍊",
3: "Pears 🍐"
};
console.log("Here is the first fruit: ", fruitdictionary[1]);
console.log("Here is the second fruit: ", fruitdictionary[2]);
console.log("Here is the third fruit: ", fruitdictionary[3])
<IPython.core.display.Javascript object>
Popcorn Hack #2
import random
def play_game():
score = 0
while True:
num1 = random.randint(1, 20)
num2 = random.randint(1, 20)
operator = random.choice(['+', '-', '*', '/'])
if operator == '+':
correct_answer = num1 + num2
elif operator == '-':
correct_answer = num1 - num2
elif operator == '/':
correct_answer = num1 // num2
else:
correct_answer = num1 * num2
print(f"What is {num1} {operator} {num2}?")
player_answer = input("Your answer (or type 'q' to quit): ")
if player_answer.lower() == 'q':
break
if int(player_answer) == correct_answer:
print("Correct!")
score += 1
else:
print(f"Wrong.")
print(f"Your final score is: {score}")
play_game()
What is 2 - 14?
Correct!
What is 18 * 14?
Wrong.
What is 9 / 5?
Wrong.
What is 1 / 8?
Correct!
What is 6 + 9?
Correct!
What is 4 + 13?
Your final score is: 3
Popcorn Hack #3
def temp_changer():
temperature = float(input("Enter the temperature: "))
conversion_type = input("Convert to Celsius or Fahrenheit? ").strip().upper()
if conversion_type == "C":
pass
celsius = (temperature - 32) * (5 / 9)
print(f"{temperature}°F is has been converted to {celsius:.2f}°C")
elif conversion_type == "F":
pass
fahrenheit = (temperature * (9 / 5)) + 32
print(f"{temperature}°C has been converted to {fahrenheit:.2f}°F")
else:
print("You cannot enter anything other than 'C' for Celsius or 'F' for Fahrenheit.")
temp_changer()
12.0°F is has been converted to -11.11°C
Python Homework
shopping_list = []
total_cost = 0
while True:
item_name = input("Enter the name of the item, if you are done adding type 'done': ")
if item_name.lower() == 'done':
break
item_price = int(input("Enter the price of the item: "))
shopping_list.append(item_name)
total_cost += item_price
print(shopping_list)
print(total_cost)
['bananas', 'apples']
37
Javascript Homework
%%js
let ingredients = [];
let conversionrates = {
cuptotablespoons: 16,
tablespoontoteaspoons: 3,
cuptoteaspoons: 48
};
while (true) {
let ingredientname = prompt("Please enter the name of the ingredient, and if you're done, type 'done': ").toLowerCase();
if (ingredientname === 'done') {
break;
}
let quantity = parseFloat(prompt("Enter the quantity of the ingredient:"));
let currentunit = prompt("Enter the current unit (cups, tablespoons, teaspoons):").toLowerCase();
let desiredunit = prompt("Enter the desired unit to convert to (cups, tablespoons, teaspoons):").toLowerCase();
let convertedquantity;
if (currentunit === 'cups') {
if (desiredunit === 'tablespoons') {
convertedquantity = quantity * conversionrates.cuptotablespoons;
} else if (desiredunit === 'teaspoons') {
convertedquantity = quantity * conversionrates.cuptoteaspoons;
}
} else if (currentunit === 'tablespoons') {
if (desiredunit === 'cups') {
convertedquantity = quantity / conversionrates.cuptotablespoons;
} else if (desiredunit === 'teaspoons') {
convertedquantity = quantity * conversionrates.tablespoontoteaspoons;
}
} else if (currentunit === 'teaspoons') {
if (desiredunit === 'cups') {
convertedquantity = quantity / conversionrates.cuptoteaspoons;
} else if (desiredunit === 'tablespoons') {
convertedquantity = quantity / conversionrates.tablespoontoteaspoons;
}
}
ingredients.push({
name: ingredientname,
originalquantity: `${quantity} ${currentunit}`,
convertedquantity: `${convertedquantity.toFixed(2)} ${desiredunit}`
});
}
console.log("Converted ingredient list:");
ingredients.forEach(ingredient => {
console.log(`${ingredient.name}: ${ingredient.originalquantity} -> ${ingredient.convertedquantity}`);
});