Skip to the content.

problems

Quick launch into Variables, Functions, Arrays, Classes, Objects.

JavaScript

Hack 1, 3.3.3: Fibonacci

# Simple Fibonacci sequence function with slight changes

def fibonacci(n):
    # Base cases: return n for 0 or 1, since Fibonacci(0) = 0 and Fibonacci(1) = 1
    if n <= 1:
        return n
    
    # Starting values for Fibonacci sequence
    a, b = 0, 1
    
    # Loop from 2 to n (since Fibonacci(0) and Fibonacci(1) are already known)
    for i in range(2, n+1):
        # Calculate the next Fibonacci number
        a, b = b, a + b  # a becomes the previous b, and b becomes the next number
    
    return b

# Example: Find the 7th Fibonacci number
n = 7
print(f"The {n}th Fibonacci number is: {fibonacci(n)}")

The 7th Fibonacci number is: 13

Hack 2, 3.3.4: Js operations

# Function for basic arithmetic operations in Python

def basic_operations(a, b):
    # Perform basic arithmetic operations
    sum_result = a + b
    difference = a - b
    product = a * b
    quotient = a / b
    remainder = a % b
    
    # Print out the results
    print(f"Addition: {a} + {b} = {sum_result}")
    print(f"Subtraction: {a} - {b} = {difference}")
    print(f"Multiplication: {a} * {b} = {product}")
    print(f"Division: {a} / {b} = {quotient}")
    print(f"Modulus: {a} % {b} = {remainder}")

# Example usage
basic_operations(10, 5)

Addition: 10 + 5 = 15
Subtraction: 10 - 5 = 5
Multiplication: 10 * 5 = 50
Division: 10 / 5 = 2.0
Modulus: 10 % 5 = 0

Hack 3, 3.5.3: PYTHON GAME plus Truth

# Simple game using De Morgan's Law
def can_go_outside(is_raining, is_cold):
    # De Morgan's Law: Not (raining and cold) is the same as not raining or not cold
    if not (is_raining and is_cold):
        return "It's safe to go outside!"
    else:
        return "Stay inside, it's raining and cold!"

# Example interaction
def play_game():
    print("Welcome to the 'Can you go outside?' game!")
    
    # User input to determine the weather
    raining_input = input("Is it raining? (yes/no): ").lower() == "yes"
    cold_input = input("Is it cold? (yes/no): ").lower() == "yes"
    
    # Check if the player can go outside using De Morgan's Law
    result = can_go_outside(raining_input, cold_input)
    
    print(result)

# Play the game
play_game()


Welcome to the 'Can you go outside?' game!
Stay inside, it's raining and cold!

Popcorn Hack 4, 3.5.4: vowel checkers

    # Function to check if a character is a vowel
def check_vowel(char):
    vowels = 'aeiou'  # Define vowels
    if char.lower() in vowels:  # Check if the character is in the vowel list (ignores case)
        print(f"The character '{char}' is a vowel.")
    else:
        print(f"The character '{char}' is not a vowel.")

# Prompt the user for input
char = input("Enter a character: ")

# Check if input is a single character
if len(char) == 1:
    check_vowel(char)  # Call the function
else:
    print("Please enter a single character.")  # Error if more than one character is entered

  Cell In[8], line 3
    vowels = 'aeiou'  # Define vowels
    ^
IndentationError: expected an indented block after function definition on line 2