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
66 changes: 66 additions & 0 deletions question1.py
Original file line number Diff line number Diff line change
@@ -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)
40 changes: 40 additions & 0 deletions question2.py
Original file line number Diff line number Diff line change
@@ -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)

16 changes: 16 additions & 0 deletions question3.py
Original file line number Diff line number Diff line change
@@ -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"`




31 changes: 31 additions & 0 deletions question4.py
Original file line number Diff line number Diff line change
@@ -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))
2 changes: 2 additions & 0 deletions tempCodeRunnerFile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
for _ in range(5000):
# random