From ae285de55863f94f70c7b1568ea5cbdb8cff39e7 Mon Sep 17 00:00:00 2001 From: 2007aman Date: Thu, 19 Feb 2026 17:33:59 +0530 Subject: [PATCH 1/2] improved dice class logic and added validation --- dice.py | 64 ++++++++++++++++++++++++++------------------------------- 1 file changed, 29 insertions(+), 35 deletions(-) diff --git a/dice.py b/dice.py index a2e5c12f99b..7f05f277683 100644 --- a/dice.py +++ b/dice.py @@ -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()}") From cb894596fb20308e2d6ab0ced9d643576db8e77a Mon Sep 17 00:00:00 2001 From: 2007aman Date: Tue, 24 Feb 2026 20:46:35 +0530 Subject: [PATCH 2/2] updated the logic in Armstrong_number.py file --- Armstrong_number.py | 45 +++++++++++++++++---------------------------- 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/Armstrong_number.py b/Armstrong_number.py index 9c73522992c..a5b02293aaa 100644 --- a/Armstrong_number.py +++ b/Armstrong_number.py @@ -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")