Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 17 additions & 28 deletions Armstrong_number.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
"""
In number theory, a narcissistic number (also known as a pluperfect digital invariant (PPDI), an Armstrong number (after Michael F. Armstrong) or a plus perfect number),
in a given number base b, is a number that is the total of its own digits each raised to the power of the number of digits.
Source: https://en.wikipedia.org/wiki/Narcissistic_number
NOTE:
this scripts only works for number in base 10
"""
def is_armstrong_number(number: str) -> bool:
"""Check if a number (as a string) is a narcissistic/Armstrong number."""
# Logic: Get the exponent (number of digits)
exponent = len(number)

# Logic: Sum each digit raised to the power in a single line
# This uses a generator, which is memory efficient.
total = sum(int(digit) ** exponent for digit in number)

# Return the boolean result instead of printing
return total == int(number)

# --- Main execution ---
user_input = input("Enter the number: ")

def is_armstrong_number(number: str):
total: int = 0
exp: int = len(
number
) # get the number of digits, this will determinate the exponent

digits: list[int] = []
for digit in number:
digits.append(int(digit)) # get the single digits
for x in digits:
total += x**exp # get the power of each digit and sum it to the total

# display the result
if int(number) == total:
print(number, "is an Armstrong number")
else:
print(number, "is not an Armstrong number")


number = input("Enter the number : ")
is_armstrong_number(number)
if is_armstrong_number(user_input):
print(f"{user_input} is an Armstrong number")
else:
print(f"{user_input} is not an Armstrong number")
64 changes: 29 additions & 35 deletions dice.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,39 @@
# Script Name : dice.py
# Author : Craig Richards
# Created : 05th February 2017
# Last Modified :
# Version : 1.0

# Modifications :

# Description : This will randomly select two numbers,
# like throwing dice, you can change the sides of the dice if you wish

import random


class Die(object):
# A dice has a feature of number about how many sides it has when it's
# established,like 6.
def __init__(self):
self.sides = 6

"""because a dice contains at least 4 planes.
So use this method to give it a judgement when you need
to change the instance attributes.
class Die:
"""
A class used to represent a multi-sided die.
Attributes:
sides (int): The number of sides on the die (default is 6).
"""

def set_sides(self, sides_change):
if sides_change >= 4:
if sides_change != 6:
print("change sides from 6 to ", sides_change, " !")
def __init__(self, sides=6):
"""Initializes the die. Defaults to 6 sides if no value is provided."""
self.sides = 6 # Internal default
self.set_sides(sides)

def set_sides(self, num_sides):
"""
Validates and sets the number of sides.
A physical die must have at least 4 sides.
"""
if isinstance(num_sides, int) and num_sides >= 4:
if num_sides != self.sides:
print(f"Changing sides from {self.sides} to {num_sides}!")
else:
# added else clause for printing a message that sides set to 6
print("sides set to 6")
self.sides = sides_change
print(f"Sides already set to {num_sides}.")
self.sides = num_sides
else:
print("wrong sides! sides set to 6")
print(f"Invalid input: {num_sides}. Keeping current value: {self.sides}")

def roll(self):
"""Returns a random integer between 1 and the number of sides."""
return random.randint(1, self.sides)


d = Die()
d1 = Die()
d.set_sides(4)
d1.set_sides(4)
print(d.roll(), d1.roll())
# --- Example Usage ---
if __name__ == "__main__":
d1 = Die(4) # Initialize directly with 4 sides
d2 = Die(12) # A Dungeons & Dragons classic
print(f"Roll Result: D{d1.sides} -> {d1.roll()}, D{d2.sides} -> {d2.roll()}")