diff --git a/Dice.py b/Dice.py new file mode 100644 index 0000000..925ca4d --- /dev/null +++ b/Dice.py @@ -0,0 +1,44 @@ +#Dice Percentage +#Create an array with 6 elements named dice. Fill this array with the value zero. +# Generate a random number with a value between 1 and 6 (just like a dice) in a repetition 5000 times. +# If the value is 1, increase the element 0 in the array by 1, +# the same applies to the values 2, 3, 4, 5 and 6. +# The dice[0] element indicates the number of times value 1 has occurred. +# Or in general: dice[x-1] indicates the number of times that x has been thrown. +# At the end of the repetition, print the contents of the array as percentages with 2 decimal places. +# For example; "Percentage of throws of value 3 = 16.28%" +import random +dice = [0,0,0,0,0,0] +r = int(input('Please Enter the Number of Repetition :')) + +def percentage(part, whole): + Percentage = 100 * float(part)/float(whole) + return str(Percentage) +' %' + +def dice_percentage(): + x = [random.randint(1,6) for x in range(r)] + for i in x: + if i ==1: + dice[0]+=1 + elif i ==2: + dice[1]+=1 + elif i ==3: + dice[2]+=1 + elif i ==4: + dice[3]+=1 + elif i ==5: + dice[4]+=1 + elif i ==6: + dice[5]+=1 +def dice_print(): + print('Percentage of throws of value 1 =',percentage(dice[0], r)) + print('Percentage of throws of value 2 =',percentage(dice[1], r)) + print('Percentage of throws of value 3 =',percentage(dice[2], r)) + print('Percentage of throws of value 4 =',percentage(dice[3], r)) + print('Percentage of throws of value 5 =',percentage(dice[4], r)) + print('Percentage of throws of value 6 =',percentage(dice[5], r)) + +dice_percentage() +print(dice) +dice_print() + diff --git a/GCD.py b/GCD.py new file mode 100644 index 0000000..476e916 --- /dev/null +++ b/GCD.py @@ -0,0 +1,21 @@ +#As a user, I want to use a program which can calculate the greatest common divisor (GCD) of my inputs. +# Acceptance Criteria: +# Ask user the enter the number of inputs (n). +# Ask user to enter n input numbers one by one. +# Use try/except blocks to verify input entries and warn the user for Nan or non numerical inputs. +# Calculate the greatest common divisor (GCD) of n numbers. +# Use gcd function in module of math. +from math import gcd +A = [] +n = int(input('Please Enter the Number of Inputs :')) +i = 1 +while i<=n: + try: + num = int(input('Please Enter the Number:')) + A.append(num) + i+=1 + except: + print('Please Enter a Numeric Input :') + i=i +print(A) +print('Greatest Common Divisor of numbers',A,'is',gcd(*A)) \ No newline at end of file diff --git a/PasswordCheck.py b/PasswordCheck.py new file mode 100644 index 0000000..3d38140 --- /dev/null +++ b/PasswordCheck.py @@ -0,0 +1,40 @@ +# Write a Python program to check the validity of a password (input from users) +# Rules : +# At least 1 letter between [a-z] and 1 letter between [A-Z] +# At least 1 number between [0-9]. +# At least 1 character from [$#@]. +# Minimum length 6 characters. +# Maximum length 16 characters. +# If password is not valid throw ValueError with a proper error message for each rule. +# If the password is valid print a success message. Use some from raise, except, assert, else +# and finally keywords. + +import re +x = True +while x: + try: + password = input('Please Enter a Password :') + if len(password)<6 or len(password)>16: + raise PasswoordLengthError ("Your Password Should have minimum 6,maximum 16 characters.") + elif not re.search("[0-9]",password): + raise DigitUseError ("Please Use At Least One Digit") + elif not re.search("[a-z]",password): + raise LowercaseUseError ("Please Use At Least One Lowercase Letter") + elif not re.search("[A-Z]",password): + raise UppercaseUseError ("Please Use At Least One Uppercase Letter") + elif not re.search("['$','#','@']",password): + raise SpecialcaseUseError ("Please Use At Least One of the '$','#','@' characters") + except PasswoordLengthError as e: + print("Invalid Entry!..",e) + except DigitUseError as e: + print("Invalid Entry!..",e) + except LowercaseUseError as e: + print("Invalid Entry!..",e) + except UppercaseUseError as e: + print("Invalid Entry!..",e) + except SpecialcaseUseError as e: + print("Invalid Entry!..",e) + else: + print('Password is Valid') + finally: + print('Password Check Complete') \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..683840a --- /dev/null +++ b/main.py @@ -0,0 +1,2 @@ +from my_dice import rollDice +rollDice() \ No newline at end of file diff --git a/my_dice.py b/my_dice.py new file mode 100644 index 0000000..a1d0dee --- /dev/null +++ b/my_dice.py @@ -0,0 +1,15 @@ +# Create a Python module called my_dice.py and export the code you wrote in question 2 into a function +# called rollDice(number). +# Changes: +# Instead of repetition of 5000 times, makes a repetition of times of given number variable. +# Instead of printing, return the array of percentages. +# Then create a new module called main.py. Gets an input from the user using +# "Enter repetition number: ". Then call rollDice method with this user input. +# Lastly, print each probability. E.g. "The probability of 0 is 16.20" + +def rollDice(): + from Dice import percentage + from Dice import dice_percentage + from Dice import dice_print + dice_percentage() + dice_print() \ No newline at end of file