From f64120c91af9c40dae4b65555d1ecf40fad3a7a3 Mon Sep 17 00:00:00 2001 From: TheRamy <122477951+TheRamy@users.noreply.github.com> Date: Tue, 7 Feb 2023 07:25:25 +0100 Subject: [PATCH] Week4 Assignment - Ramy --- 1.py | 23 ++++++++++++++++++++ 2.py | 17 +++++++++++++++ 3.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 4.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++ func_add.py | 4 ++++ func_divide.py | 5 +++++ func_multiply.py | 5 +++++ func_subtract.py | 4 ++++ 8 files changed, 166 insertions(+) create mode 100644 1.py create mode 100644 2.py create mode 100644 3.py create mode 100644 4.py create mode 100644 func_add.py create mode 100644 func_divide.py create mode 100644 func_multiply.py create mode 100644 func_subtract.py diff --git a/1.py b/1.py new file mode 100644 index 0000000..a602f52 --- /dev/null +++ b/1.py @@ -0,0 +1,23 @@ +# Write a Python program to create a file where all letters of English alphabet(uppercase and lowercase both) are listed by specified number of letters on each line. + + +import string + +def print_alphabet_per_line(n) : + + with open("letters.txt", "w") as f: + + alphabet = string.ascii_uppercase + string.ascii_lowercase + + for i in range(0, len(alphabet), n) : + + letters = alphabet[i:i + n] + "\n" + f.writelines(letters) + + + + +print_alphabet_per_line(5) + + + diff --git a/2.py b/2.py new file mode 100644 index 0000000..468f08e --- /dev/null +++ b/2.py @@ -0,0 +1,17 @@ +# Write a Python program to generate 26 text files named A.txt, B.txt, and so on up to Z.txt. + + +import string + +def create_files_A_to_Z () : + + A_to_Z = string.ascii_uppercase + + for x in A_to_Z : + #print (x) + with open(x + ".txt", "w") as f: + f.writelines ("") + + +create_files_A_to_Z() + diff --git a/3.py b/3.py new file mode 100644 index 0000000..a4968de --- /dev/null +++ b/3.py @@ -0,0 +1,55 @@ +""" As a user, I want to use a program which can calculate the least common multiple (L.C.M.) of four numbers. So that I can find the least common multiple (L.C.M.) of my inputs. + +**Acceptance Criteria:** + +* Ask user to enter the four numbers. +* Use try/except blocks to verify input entries and warn the user for Nan or non numerical inputs. +* Calculate the least common multiple (L.C.M.) of four numbers +* Use gcd function in module of math + """ + +from math import gcd +from functools import reduce +import math + + +def calc_GCD (n1, n2): + + return (n1 * n2) // math.gcd(n1, n2) + +def calc_LCM (*args): + + return reduce(calc_GCD, args) # using recursion with reduce() + + + +try: + + n_one = int(input("enter number 1: ")) + n_two = int(input("enter number 2: ")) + n_three = int(input("enter number 3: ")) + n_four = int(input("enter number 4: ")) + +except ValueError : + + print ("You used a non numerical input!") + +else: + + #gcd = math.gcd( math.gcd(n_one, n_two) , math.gcd(n_three, n_four) ) + #print (gcd) + + lcm = calc_LCM ( n_one , n_two , n_three , n_four ) + + print ("the least common multiple (L.C.M.) is: " , end="") + print (lcm) + + + + # for testing - 12 15 75 32 . LCM = 2400 + + + + + + diff --git a/4.py b/4.py new file mode 100644 index 0000000..07e8513 --- /dev/null +++ b/4.py @@ -0,0 +1,53 @@ +""" +As a user, I want to use a program which can calculate basic +mathematical operations. So that I can add, subtract, multiply or divide my inputs. + +**Acceptance Criteria:** + +* The calculator must support the Addition, Subtraction, Multiplication and Division operations. +* Define four functions in four files for each of them, with two float numbers as parameters. +* To calculate the answer, use math.ceil() and get the next integer value greater than the result +* Create a menu using the print command with the respective options and take an input user_choosen_option from the user. +* Using if/elif statements for cases and call the appropriate functions. +* Use try/except blocks to verify input entries and warn the user for incorrect inputs. +* Ask user if calculate numbers again. To implement this, take the input from user `Y` or `N`. + """ + + +from func_add import * +from func_divide import * +from func_multiply import * +from func_subtract import * + + +while True: + + print("What do you want to do: ") + print("(a) add") + print("(s) subtract") + print("(m) multiply") + print("(d) divide") + + user_choosen_option = input("Enter first letter... (a)dd, (s)ubtract, (m)ultiply, or (d)ivide: ") + + try: + num1 = float(input("enter the first number: ")) + num2 = float(input("enter the second number: ")) + if user_choosen_option == 'a': + print("result: ", add(num1, num2)) + + elif user_choosen_option == 's': + print("result: ", subtract(num1, num2)) + + elif user_choosen_option == 'm': + print("result: ", multiply(num1, num2)) + + elif user_choosen_option == 'd': + print("result: ", divide(num1, num2)) + + except ValueError: + print("there's an input error") + + calculate_again = input("do you want to calculate something else? (y)es or (n)o? ") + if calculate_again != "y": + break diff --git a/func_add.py b/func_add.py new file mode 100644 index 0000000..291c1df --- /dev/null +++ b/func_add.py @@ -0,0 +1,4 @@ +import math + +def add(a, b): + return math.ceil(a + b) \ No newline at end of file diff --git a/func_divide.py b/func_divide.py new file mode 100644 index 0000000..20e23b3 --- /dev/null +++ b/func_divide.py @@ -0,0 +1,5 @@ +import math + +def divide(a, b): + return math.ceil(a / b) + \ No newline at end of file diff --git a/func_multiply.py b/func_multiply.py new file mode 100644 index 0000000..3f4ba3c --- /dev/null +++ b/func_multiply.py @@ -0,0 +1,5 @@ +import math + +def multiply(a, b): + return math.ceil(a * b) + \ No newline at end of file diff --git a/func_subtract.py b/func_subtract.py new file mode 100644 index 0000000..f2dc57f --- /dev/null +++ b/func_subtract.py @@ -0,0 +1,4 @@ +import math + +def subtract(a, b): + return math.ceil(a - b) \ No newline at end of file