From ef165afbe974292ca3d694b1095d3d1cbbfe9407 Mon Sep 17 00:00:00 2001 From: gmaknl21 Date: Tue, 7 Feb 2023 15:54:07 +0100 Subject: [PATCH] Week4-File-I-O-Exception-Handling G --- README.md | 35 --------------------------- alphabet.txt | 13 +++++++++++ homework_1.py | 17 ++++++++++++++ homework_2.py | 23 ++++++++++++++++++ homework_3.py | 48 +++++++++++++++++++++++++++++++++++++ homework_4.py | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++ letters/A.txt | 1 + letters/B.txt | 1 + letters/C.txt | 1 + letters/D.txt | 1 + letters/E.txt | 1 + letters/F.txt | 1 + letters/G.txt | 1 + letters/H.txt | 1 + letters/I.txt | 1 + letters/J.txt | 1 + letters/K.txt | 1 + letters/L.txt | 1 + letters/M.txt | 1 + letters/N.txt | 1 + letters/O.txt | 1 + letters/P.txt | 1 + letters/Q.txt | 1 + letters/R.txt | 1 + letters/S.txt | 1 + letters/T.txt | 1 + letters/U.txt | 1 + letters/V.txt | 1 + letters/W.txt | 1 + letters/X.txt | 1 + letters/Y.txt | 1 + letters/Z.txt | 1 + my_modules.py | 15 ++++++++++++ 33 files changed, 207 insertions(+), 35 deletions(-) delete mode 100644 README.md create mode 100644 alphabet.txt create mode 100644 homework_1.py create mode 100644 homework_2.py create mode 100644 homework_3.py create mode 100644 homework_4.py create mode 100644 letters/A.txt create mode 100644 letters/B.txt create mode 100644 letters/C.txt create mode 100644 letters/D.txt create mode 100644 letters/E.txt create mode 100644 letters/F.txt create mode 100644 letters/G.txt create mode 100644 letters/H.txt create mode 100644 letters/I.txt create mode 100644 letters/J.txt create mode 100644 letters/K.txt create mode 100644 letters/L.txt create mode 100644 letters/M.txt create mode 100644 letters/N.txt create mode 100644 letters/O.txt create mode 100644 letters/P.txt create mode 100644 letters/Q.txt create mode 100644 letters/R.txt create mode 100644 letters/S.txt create mode 100644 letters/T.txt create mode 100644 letters/U.txt create mode 100644 letters/V.txt create mode 100644 letters/W.txt create mode 100644 letters/X.txt create mode 100644 letters/Y.txt create mode 100644 letters/Z.txt create mode 100644 my_modules.py diff --git a/README.md b/README.md deleted file mode 100644 index 44f5fa1..0000000 --- a/README.md +++ /dev/null @@ -1,35 +0,0 @@ -# Class8-Week4-File-I-O-Exception-Handling - - -## Letters for each line - -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. - -## Question 2 - -Write a Python program to generate 26 text files named A.txt, B.txt, and so on up to Z.txt. - -## LCM - -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 - -## 4- Mis Calculator - -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 choice 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`. diff --git a/alphabet.txt b/alphabet.txt new file mode 100644 index 0000000..52f1b36 --- /dev/null +++ b/alphabet.txt @@ -0,0 +1,13 @@ +abcd +efgh +ijkl +mnop +qrst +uvwx +yzAB +CDEF +GHIJ +KLMN +OPQR +STUV +WXYZ diff --git a/homework_1.py b/homework_1.py new file mode 100644 index 0000000..82e6101 --- /dev/null +++ b/homework_1.py @@ -0,0 +1,17 @@ +### Question 1 +"""Letters for each line +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. +""" + +letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + +def letters_file_line(n): + + with open("alphabet.txt", "w") as f: + for i, letter in enumerate(letters): + f.write(letter) + if (i + 1) % n == 0: + f.write("\n") + +letters_file_line(4) \ No newline at end of file diff --git a/homework_2.py b/homework_2.py new file mode 100644 index 0000000..3626fb7 --- /dev/null +++ b/homework_2.py @@ -0,0 +1,23 @@ +"""Question 2 +Write a Python program to generate 26 text files named A.txt, B.txt, and so on up to Z.txt.""" + +import string, os # import string and os.path + +""" First define a function file_generaters which generates a text file for each letter of the uppercase alphabet. """ + +def file_generaters(): + """if a directory named "letters" exists and + creates it if it doesn't using the os.makedirs function.""" + + if not os.path.exists("letters"): + os.makedirs("letters") + + """This for loop will for each leter in the upercase alphabet, defined by string.ascii_uppercase, the code + creates a text file with the name of the letter and writes to the letters as its contents using the with statement + and the open functiion. """ + + for letter in string.ascii_uppercase: + + with open(letter + ".txt", "w") as f: + f.writelines(letter) +file_generaters() # function is called to generate the file names diff --git a/homework_3.py b/homework_3.py new file mode 100644 index 0000000..ed8e5a7 --- /dev/null +++ b/homework_3.py @@ -0,0 +1,48 @@ +### Question 3 +"""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 +""" + +import math + +"""The 'main_func' function takes four numbers as input and returns the L.C.M. of these numbers.""" +def main_func(num1,num2,num3,num4): + # Code that should be executed when the script is run as the main program + """The 'math.gcd' function from the 'math' library to calculate the greatest common divisor of two numbers, + which is then used to calculate the L.C.M.""" + return num1 * num2 * num3 * num4 // math.gcd(math.gcd(num1, num2), math.gcd(num3, num4)) + + +"""The code is executed only when the script is run as the main program, as +determined by the 'if name == 'main':' statement. Within this block, user input is obtained +for the four numbers and the 'main_func' is called with these inputs. The result is then printed to +the console. Exception handling is used to ensure that the input is a valid number. +""" +if __name__ == '__main__': + + while True: + try: + n1 = int(input("Enter the first number:")) + n2 = int(input("Enter the seconde number:")) + n3 = int(input("Enter the third number:")) + n4 = int(input("Enter the fourth number:")) + break + except: + print("Invalid input. Pleass enter a number.") + + r = main_func(n1, n2, n3, n4) + print("The least common multiple (L.C.M.) of four numbers:", r) + + + + + + + diff --git a/homework_4.py b/homework_4.py new file mode 100644 index 0000000..d1c7c30 --- /dev/null +++ b/homework_4.py @@ -0,0 +1,65 @@ +### Question 4 +""""" Mis Calculator +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 choice 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 my_modules import * + +# Define the main function +def main_func(): + + while True: + + choice = input(""" Enter Your choice if you want to + for Addition write = add, + for Subtraction= sub, + for Multiplication = mult, + for Division= div, + :> """).lower() + + # Try to retrieve the two numbers from the user input + try: + num1 = float(input("Enter the first number: ")) + num2 = float(input("Enter the second number: ")) + + except ValueError: + print("Invalid input. Please enter a number.") + continue + + # Check the user's choice and call the appropriate function from the imported module + if choice == "add": + r = add(num1, num2) + print("The result is ", r) + elif choice == "sub": + r = subtract(num1, num2) + print("The result is ", r) + elif choice == "mult": + r = multiply(num1, num2) + print("The result is ", r) + elif choice == "div": + r = divide(num1, num2) + print("The result is ", r) + # If the user's choice is invalid, show an error message and prompt again + else: + print("Invalid choice. Please try again.") + continue + + # Ask the user if they want to calculate again + t = input("Do you want to calculate again? (y/n): ").lower() + # If the user doesn't want to calculate again, break out of the loop + if t != 'y': + break + +# Execute the main function when the script is run as the main program +if __name__ == "__main__": + main_func() \ No newline at end of file diff --git a/letters/A.txt b/letters/A.txt new file mode 100644 index 0000000..8c7e5a6 --- /dev/null +++ b/letters/A.txt @@ -0,0 +1 @@ +A \ No newline at end of file diff --git a/letters/B.txt b/letters/B.txt new file mode 100644 index 0000000..7371f47 --- /dev/null +++ b/letters/B.txt @@ -0,0 +1 @@ +B \ No newline at end of file diff --git a/letters/C.txt b/letters/C.txt new file mode 100644 index 0000000..96d80cd --- /dev/null +++ b/letters/C.txt @@ -0,0 +1 @@ +C \ No newline at end of file diff --git a/letters/D.txt b/letters/D.txt new file mode 100644 index 0000000..02358d2 --- /dev/null +++ b/letters/D.txt @@ -0,0 +1 @@ +D \ No newline at end of file diff --git a/letters/E.txt b/letters/E.txt new file mode 100644 index 0000000..9fb75b8 --- /dev/null +++ b/letters/E.txt @@ -0,0 +1 @@ +E \ No newline at end of file diff --git a/letters/F.txt b/letters/F.txt new file mode 100644 index 0000000..c137216 --- /dev/null +++ b/letters/F.txt @@ -0,0 +1 @@ +F \ No newline at end of file diff --git a/letters/G.txt b/letters/G.txt new file mode 100644 index 0000000..4fc3fe1 --- /dev/null +++ b/letters/G.txt @@ -0,0 +1 @@ +G \ No newline at end of file diff --git a/letters/H.txt b/letters/H.txt new file mode 100644 index 0000000..8ac2eb5 --- /dev/null +++ b/letters/H.txt @@ -0,0 +1 @@ +H \ No newline at end of file diff --git a/letters/I.txt b/letters/I.txt new file mode 100644 index 0000000..b4158c4 --- /dev/null +++ b/letters/I.txt @@ -0,0 +1 @@ +I \ No newline at end of file diff --git a/letters/J.txt b/letters/J.txt new file mode 100644 index 0000000..22aac29 --- /dev/null +++ b/letters/J.txt @@ -0,0 +1 @@ +J \ No newline at end of file diff --git a/letters/K.txt b/letters/K.txt new file mode 100644 index 0000000..449e49e --- /dev/null +++ b/letters/K.txt @@ -0,0 +1 @@ +K \ No newline at end of file diff --git a/letters/L.txt b/letters/L.txt new file mode 100644 index 0000000..083b700 --- /dev/null +++ b/letters/L.txt @@ -0,0 +1 @@ +L \ No newline at end of file diff --git a/letters/M.txt b/letters/M.txt new file mode 100644 index 0000000..ef6bce1 --- /dev/null +++ b/letters/M.txt @@ -0,0 +1 @@ +M \ No newline at end of file diff --git a/letters/N.txt b/letters/N.txt new file mode 100644 index 0000000..2f94675 --- /dev/null +++ b/letters/N.txt @@ -0,0 +1 @@ +N \ No newline at end of file diff --git a/letters/O.txt b/letters/O.txt new file mode 100644 index 0000000..60a89ed --- /dev/null +++ b/letters/O.txt @@ -0,0 +1 @@ +O \ No newline at end of file diff --git a/letters/P.txt b/letters/P.txt new file mode 100644 index 0000000..675f43a --- /dev/null +++ b/letters/P.txt @@ -0,0 +1 @@ +P \ No newline at end of file diff --git a/letters/Q.txt b/letters/Q.txt new file mode 100644 index 0000000..866ad47 --- /dev/null +++ b/letters/Q.txt @@ -0,0 +1 @@ +Q \ No newline at end of file diff --git a/letters/R.txt b/letters/R.txt new file mode 100644 index 0000000..ac044e5 --- /dev/null +++ b/letters/R.txt @@ -0,0 +1 @@ +R \ No newline at end of file diff --git a/letters/S.txt b/letters/S.txt new file mode 100644 index 0000000..1db515f --- /dev/null +++ b/letters/S.txt @@ -0,0 +1 @@ +S \ No newline at end of file diff --git a/letters/T.txt b/letters/T.txt new file mode 100644 index 0000000..96583aa --- /dev/null +++ b/letters/T.txt @@ -0,0 +1 @@ +T \ No newline at end of file diff --git a/letters/U.txt b/letters/U.txt new file mode 100644 index 0000000..4f0734c --- /dev/null +++ b/letters/U.txt @@ -0,0 +1 @@ +U \ No newline at end of file diff --git a/letters/V.txt b/letters/V.txt new file mode 100644 index 0000000..d426ba8 --- /dev/null +++ b/letters/V.txt @@ -0,0 +1 @@ +V \ No newline at end of file diff --git a/letters/W.txt b/letters/W.txt new file mode 100644 index 0000000..05fdf94 --- /dev/null +++ b/letters/W.txt @@ -0,0 +1 @@ +W \ No newline at end of file diff --git a/letters/X.txt b/letters/X.txt new file mode 100644 index 0000000..500c070 --- /dev/null +++ b/letters/X.txt @@ -0,0 +1 @@ +X \ No newline at end of file diff --git a/letters/Y.txt b/letters/Y.txt new file mode 100644 index 0000000..24de910 --- /dev/null +++ b/letters/Y.txt @@ -0,0 +1 @@ +Y \ No newline at end of file diff --git a/letters/Z.txt b/letters/Z.txt new file mode 100644 index 0000000..0f13712 --- /dev/null +++ b/letters/Z.txt @@ -0,0 +1 @@ +Z \ No newline at end of file diff --git a/my_modules.py b/my_modules.py new file mode 100644 index 0000000..cedc6cf --- /dev/null +++ b/my_modules.py @@ -0,0 +1,15 @@ +# add +def add(a, b): + return a + b + +# subtract +def subtract(a, b): + return a - b + +# multiply +def multiply(a, b): + return a * b + +# divide +def divide(a, b): + return a / b