Free Friday 4/10/26

Today in class, I made a dice roller program. It asks the user how many dice they want then adds the number of die up and tells the total.

This the code for the dice.

dice_art = {
    1: ("┌─────────┐",
        "│         │",
        "│    ●    │",
        "│         │",
        "└─────────┘"),
    2: ("┌─────────┐",
        "│  ●      │",
        "│         │",
        "│      ●  │",
        "└─────────┘"),
    3: ("┌─────────┐",
        "│  ●      │",
        "│    ●    │",
        "│      ●  │",
        "└─────────┘"),
    4: ("┌─────────┐",
        "│  ●   ●  │",
        "│         │",
        "│  ●   ●  │",
        "└─────────┘"),
    5: ("┌─────────┐",
        "│  ●   ●  │",
        "│    ●    │",
        "│  ●   ●  │",
        "└─────────┘"),
    6: ("┌─────────┐",
        "│  ●   ●  │",
        "│  ●   ●  │",
        "│  ●   ●  │",
        "└─────────┘")
}

This is the code that gets random dices out of 6.

for die in range(num_of_dice):
    dice.append(random.randint(1, 6))

This is the code that prints all the dice horizontally.

for line in range(5):
    for die in dice:
        print(dice_art.get(die)[line], end ="")
    print()

This is the code to print all the dice vertically.

for die in range(num_of_dice):
    for line in dice_art.get(dice[die]):
        print(line)

This is the code that counts the die on the dice and adds them up.

for die in dice:
    total += die
print(f"total:{total}")    

I also made a encryption program. It asks the user to enter a message to encrypt and it encrypts the message.

This is the code for the letters, digits, and punctuation used to encrypt the message.

chars = " " + string.punctuation + string.digits + string.ascii_letters
chars = list(chars)
key = chars.copy()

This is the code that shuffles the keys used for the encrypted message.

random.shuffle(key)

This is the code that asks the user to enter a message to encrypt then it encrypts the message and shows the original message and encrypted message.

plain_text = input("Enter a message to encrypt: ")
cipher_text = ""

for letter in plain_text:
    index = chars.index(letter)
    cipher_text += key[index]

print(f"original message : {plain_text}")
print(f"encrypted message: {cipher_text}")

This is the code that decrypts the message.

cipher_text = input("Enter a message to encrypt: ")
plain_text = ""

for letter in cipher_text:
    index = key.index(letter)
    plain_text += chars[index]

print(f"encrypted message: {cipher_text}")
print(f"original message : {plain_text}")

CATEGORIES:

Tags:

No Responses

Leave a Reply

Your email address will not be published. Required fields are marked *