diff --git a/math/geoMean.py b/math/geoMean.py index cc43cd6..14c05e2 100644 --- a/math/geoMean.py +++ b/math/geoMean.py @@ -1,19 +1,32 @@ -# Add path, encoding +#!/usr/bin -import math +# Geometric Mean: https://en.wikipedia.org/wiki/Geometric_mean +def geoMean(*args): + '''Calculates and returns geometric mean of list of integers''' + n = len(args) -# Geometric Mean: -# Calculates the geometric mean of -# two numbers -# formula: f(x, y) = square_root(x*y) + prod = 1 + for i in range(n): + prod = prod*args[i] -geoMean = lambda x, y: math.sqrt(x*y) + gmean = prod**(1/n) + return gmean -while True: - if input("Start [Y/n]? ").strip().lower() == "y": - print(" [Res] = " + str(geoMean(float(input("\nX? ")), float(input("Y? ")))) + "\n\n") - else: - print("\n\nGoodBye!") - break +if __name__ == "__main__": + + flag = 1 + while(flag): + + # Take input from user + numbers = list(map(int, input('Enter integers separated by space: ').split())) + + # call function + g = geoMean(*numbers) #list unpacking + + # display output + print('The geometric mean of {} is {:.4}\n'.format(numbers, g)) + + flag = int(input('Enter 1 to repeat, 0 to quit: ')) + print() diff --git a/piggyBank.py b/piggyBank.py index 4a10c8c..8e56993 100644 --- a/piggyBank.py +++ b/piggyBank.py @@ -1,57 +1,72 @@ -# piggy bank -# pre code -money = 0 - -# function to add money to current amount -def addMoney(): - print(" ") - userAdd = float(raw_input("Add money : ")) - print(" ") - money = money + userAdd - print("After adding current Money you have is " + str(money) + " rupees") - -# function to withdraw money from current amount -def withdrawMoney(): - print(" ") - userWithdraw = float(raw_input("Add money : ")) - print(" ") - money = money + userWithdraw - print("After adding current Money you have is " + str(money) + " rupees") - -# function to display current amount -def currentMoney(): - print(" ") - current = "Current money you have is " + str(money) + " rupees" - -# main code -print(" ") -print("--------------------Start-------------------") -while True: - print(" ") - user = raw_input("Start or End : ") - if user.strip() == "Start": - controlPiggy = raw_input("Add Withdraw or Check : ") - if controlPiggy.strip() == "Add": - print(addMoney()) - continue - elif controlPiggy.strip() == "Withdraw": - print(withdrawMoney()) - continue - elif controlPiggy.strip() == "Check": - print(currentMoney()) - continue - else : - print(" ") - print("Invalid Input.Try again") - continue - - elif user.strip() == "End" : - print(" ") - print("------------Program Ended-----------") - print(" ") - break - - else : - print(" ") - print("Invalid Input. Try again") - continue +class PiggyBank(): + ''' Piggy Bank class ''' + + #class variable + currency = 'USD' + + def __init__(self, name=None, balance=0.0): + '''Initializes the instance ''' + self.name = name + self.balance = balance + + def deposit(self, money=0.0): + '''Add money to your account''' + self.balance += money + + def display(self): + '''Display the current balance''' + return self.balance + + def withdraw(self, money=0.0): + '''withdraw money from your account''' + self.balance -= money + + def showName(self): + '''show user name''' + return self.name + + +if __name__== "__main__": + + #Take input from user + name = input('Enter the name of the user: ') + initBal = float(input('Enter starting balance: ')) + + account = PiggyBank(name, initBal) #create an instance + + flag = 1 + while(flag): + print('\n------Press the numbers accordingly to perform the operation-----') + print('1. Deposit money\n2. Withdraw money\n3. Show current balance') + print('4. Show account name\n5. Quit') + + cmd = int(input()) + + if cmd == 1: + money = float(input('Enter the amount to deposit: ')) + account.deposit(money) + print('Current balance is {} {}'.format(account.display(), account.currency)) + + elif cmd == 2: + money = float(input('Enter the amount to withdraw: ')) + account.withdraw(money) + print('Current balance is {} {}'.format(account.display(), account.currency)) + + elif cmd == 3: + print('Current balance is {} {}'.format(account.display(), account.currency)) + + elif cmd == 4: + print('Account name: {}'.format(account.name)) + + elif cmd == 5: + while(flag): + check = input('Are you sure you want to exit? [y / n]: ') + + if check == 'y' or check == 'Y': + print('exiting the program. See you again!') + flag = 0 + else: + break + + +