From 425d1ed09dc4ff3487e71b916310015a9dded367 Mon Sep 17 00:00:00 2001 From: suyogschavan Date: Mon, 2 Jan 2023 22:51:55 +0530 Subject: [PATCH] Added some projects --- Calculator/main.py | 159 ++++++++++++++++++++++++++++++++++++++ Calculator/readme | 3 + Pascal's Triangle/main.py | 24 ++++++ Pascal's Triangle/readme | 3 + 4 files changed, 189 insertions(+) create mode 100644 Calculator/main.py create mode 100644 Calculator/readme create mode 100644 Pascal's Triangle/main.py create mode 100644 Pascal's Triangle/readme diff --git a/Calculator/main.py b/Calculator/main.py new file mode 100644 index 00000000..dcdb6aba --- /dev/null +++ b/Calculator/main.py @@ -0,0 +1,159 @@ +''' +Calculator +------------------------------------------------------------- +''' + + +import os + + +def addition(): + os.system('cls' if os.name == 'nt' else 'clear') + print('Addition') + + continue_calc = 'y' + + num_1 = float(input('Enter a number: ')) + num_2 = float(input('Enter another number: ')) + ans = num_1 + num_2 + values_entered = 2 + print(f'Current result: {ans}') + + while continue_calc.lower() == 'y': + continue_calc = (input('Enter more (y/n): ')) + while continue_calc.lower() not in ['y', 'n']: + print('Please enter \'y\' or \'n\'') + continue_calc = (input('Enter more (y/n): ')) + + if continue_calc.lower() == 'n': + break + num = float(input('Enter another number: ')) + ans += num + print(f'Current result: {ans}') + values_entered += 1 + return [ans, values_entered] + + +def subtraction(): + os.system('cls' if os.name == 'nt' else 'clear') + print('Subtraction') + + continue_calc = 'y' + + num_1 = float(input('Enter a number: ')) + num_2 = float(input('Enter another number: ')) + ans = num_1 - num_2 + values_entered = 2 + print(f'Current result: {ans}') + + while continue_calc.lower() == 'y': + continue_calc = (input('Enter more (y/n): ')) + while continue_calc.lower() not in ['y', 'n']: + print('Please enter \'y\' or \'n\'') + continue_calc = (input('Enter more (y/n): ')) + + if continue_calc.lower() == 'n': + break + num = float(input('Enter another number: ')) + ans -= num + print(f'Current result: {ans}') + values_entered += 1 + return [ans, values_entered] + + +def multiplication(): + os.system('cls' if os.name == 'nt' else 'clear') + print('Multiplication') + + continue_calc = 'y' + + num_1 = float(input('Enter a number: ')) + num_2 = float(input('Enter another number: ')) + ans = num_1 * num_2 + values_entered = 2 + print(f'Current result: {ans}') + + while continue_calc.lower() == 'y': + continue_calc = (input('Enter more (y/n): ')) + while continue_calc.lower() not in ['y', 'n']: + print('Please enter \'y\' or \'n\'') + continue_calc = (input('Enter more (y/n): ')) + + if continue_calc.lower() == 'n': + break + num = float(input('Enter another number: ')) + ans *= num + print(f'Current result: {ans}') + values_entered += 1 + return [ans, values_entered] + + +def division(): + os.system('cls' if os.name == 'nt' else 'clear') + print('Division') + + continue_calc = 'y' + + num_1 = float(input('Enter a number: ')) + num_2 = float(input('Enter another number: ')) + while num_2 == 0.0: + print('Please enter a second number > 0') + num_2 = float(input('Enter another number: ')) + + ans = num_1 / num_2 + values_entered = 2 + print(f'Current result: {ans}') + + while continue_calc.lower() == 'y': + continue_calc = (input('Enter more (y/n): ')) + while continue_calc.lower() not in ['y', 'n']: + print('Please enter \'y\' or \'n\'') + continue_calc = (input('Enter more (y/n): ')) + + if continue_calc.lower() == 'n': + break + num = float(input('Enter another number: ')) + while num == 0.0: + print('Please enter a number > 0') + num = float(input('Enter another number: ')) + ans /= num + print(f'Current result: {ans}') + values_entered += 1 + return [ans, values_entered] + + +def calculator(): + quit = False + while not quit: + results = [] + print('Simple Calculator in Python!') + print('Enter \'a\' for addition') + print('Enter \'s\' for substraction') + print('Enter \'m\' for multiplication') + print('Enter \'d\' for division') + print('Enter \'q\' to quit') + + choice = input('Selection: ') + + if choice == 'q': + quit = True + continue + + if choice == 'a': + results = addition() + print('Ans = ', results[0], ' total inputs: ', results[1]) + elif choice == 's': + results = subtraction() + print('Ans = ', results[0], ' total inputs: ', results[1]) + elif choice == 'm': + results = multiplication() + print('Ans = ', results[0], ' total inputs: ', results[1]) + elif choice == 'd': + results = division() + print('Ans = ', results[0], ' total inputs: ', results[1]) + else: + print('Sorry, invalid character') + + +if __name__ == '__main__': + calculator() \ No newline at end of file diff --git a/Calculator/readme b/Calculator/readme new file mode 100644 index 00000000..34c44a1d --- /dev/null +++ b/Calculator/readme @@ -0,0 +1,3 @@ +As one of the easy Python projects, this program creates a basic calculator application with addition, subtraction, multiplication, and division functions. + +This is one of the Python practice projects that are great for learning how to use loops, functions, conditional statements, user input, and string formatting. We’ve also used the Python os module to clear the screen after the user completes their calculation actions. \ No newline at end of file diff --git a/Pascal's Triangle/main.py b/Pascal's Triangle/main.py new file mode 100644 index 00000000..1647966f --- /dev/null +++ b/Pascal's Triangle/main.py @@ -0,0 +1,24 @@ +''' +Pascal's Triangle +------------------------------------------------------------- +Number of combinations via "n choose k" or nCk = n! / [k! * (n-k)!] +''' + + +from math import factorial + + +def pascal_triangle(n): + for i in range(n): + for j in range(n-i+1): + print(end=' ') + + for j in range(i+1): + + print(factorial(i)//(factorial(j)*factorial(i-j)), end=' ') + + print() + + +if __name__ == '__main__': + pascal_triangle(5) \ No newline at end of file diff --git a/Pascal's Triangle/readme b/Pascal's Triangle/readme new file mode 100644 index 00000000..dfbbb91f --- /dev/null +++ b/Pascal's Triangle/readme @@ -0,0 +1,3 @@ +This Python project prints out Pascal’s Triangle by utilizing conditional statements and loops. It also uses the standard library’s math module and factorial function to evaluate the ‘number of combinations’ equation used to generate the values in the triangle. + +Experiment with the seed number for the triangle to examine how the ‘combinations’ equation is used to generate successive values in the triangle. \ No newline at end of file