From d1459f99e201ec1e009140c9cb3569a76fccc81e Mon Sep 17 00:00:00 2001 From: Ali Osman Date: Sat, 9 Apr 2022 00:14:35 +0200 Subject: [PATCH] last update --- question1.py | 66 +++++++++++++++++++++++++++++++++++++++++++ question2.py | 40 ++++++++++++++++++++++++++ question3.py | 16 +++++++++++ question4.py | 31 ++++++++++++++++++++ tempCodeRunnerFile.py | 2 ++ 5 files changed, 155 insertions(+) create mode 100644 question1.py create mode 100644 question2.py create mode 100644 question3.py create mode 100644 question4.py create mode 100644 tempCodeRunnerFile.py diff --git a/question1.py b/question1.py new file mode 100644 index 0000000..2e67cb9 --- /dev/null +++ b/question1.py @@ -0,0 +1,66 @@ +# 1. Password +# 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 + +# def check_password(password): +# try: + +# if re.match('[^a-zA-Z0-9]', password): +# raise +# if re.match('[^@#$]',password): + +# raise TypeError ("At least 1 letter between [a-z] and 1 letter between [A-Z], 1 number between [0-9].") + + +# except TypeError as Type: +# print(Type) + +# # else: + +# # finally: + + +# input_password = input("Enter your paasword please: ") +# check_password(input_password) + +import re + +password = input("Enter your password: ") + + +try: + while True: + if len(password)<6 or len(password)>16: + raise ValueError("Invalid password: Minimum length 6 characters. Maximum length 16 characters..") + break + elif not re.search("[a-z]",password): + raise ValueError("Invalid password: At least 1 letter between [a-z].") + break + elif not re.search("[A-Z]",password): + raise ValueError("Invalid password: At least 1 letter between [A-Z].") + break + elif not re.search("[0-9]",password): + raise ValueError("Invalid password: At least 1 letter between [0-9].") + break + elif not re.search("[@$#]",password): + raise ValueError("Invalid password: At least 1 letter between [@$#].") + break + else: + print("Valid Password") + False + break + +except ValueError as e: + print(ValueError, e) \ No newline at end of file diff --git a/question2.py b/question2.py new file mode 100644 index 0000000..3e47796 --- /dev/null +++ b/question2.py @@ -0,0 +1,40 @@ +# 2. 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]) + + +for _ in range(5000): + x = random.choice (range(1,7)) + if x == 1: + dice[0] += 1 + elif x == 2: + dice[1] += 1 + elif x == 3: + dice[2] += 1 + elif x == 4: + dice[3] += 1 + elif x == 5: + dice[4] += 1 + elif x == 6: + dice[5] += 1 + +for i in range(1,7): + x = dice[i-1]*100/5000 + print("Persentage of throws of value {} = {:.2f}%".format(i,x)) + +print(dice) + diff --git a/question3.py b/question3.py new file mode 100644 index 0000000..59cfdfd --- /dev/null +++ b/question3.py @@ -0,0 +1,16 @@ +## 3. Basic Import +# 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"` + + + + \ No newline at end of file diff --git a/question4.py b/question4.py new file mode 100644 index 0000000..3d60595 --- /dev/null +++ b/question4.py @@ -0,0 +1,31 @@ +## 4. The Greatest Common Divisor +# 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 +import re + +n = int(input("Enter a number: ")) +numbers = [] + + +for i in range(n): + try: + x = int(input()) + numbers.append(x) + + except: + print("Non numerical inputs.") + +print(numbers) +print("gcd of the numbers: ", gcd(*numbers)) + +# https://github.com/alosoz/Class6-Python-Module-Week4.git +# x = gcd(numbers[i],numbers[j]) +# print("GCD of two numbers {} and {} is: {}".format(numbers[i],numbers[i-1],x)) diff --git a/tempCodeRunnerFile.py b/tempCodeRunnerFile.py new file mode 100644 index 0000000..63f190d --- /dev/null +++ b/tempCodeRunnerFile.py @@ -0,0 +1,2 @@ +for _ in range(5000): +# random \ No newline at end of file