diff --git a/bisectionsearch.py b/bisectionsearch.py index 0001ad9..51fc842 100644 --- a/bisectionsearch.py +++ b/bisectionsearch.py @@ -8,16 +8,16 @@ while lastGuessWas != 'c': guess = (high+low1)/2 - print "Is your secret number " + str(guess) + "?" + print "Is your secret number {}?".format(guess) lastGuessWas = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ") - if lastGuessWas != 'l' and lastGuessWas != 'h' and lastGuessWas != 'c': + if lastGuessWas not in ('l', 'h', 'c'): print 'Sorry, I did not understand your input.' elif lastGuessWas == 'l': low1 = guess elif lastGuessWas == 'h': high1 = guess elif lastGuessWas == 'c': - print "Game over. Your secret number was: " + str(guess) + print "Game over. Your secret number was: {}".format(guess) #minimum to pay off debt in 1 year @@ -25,21 +25,18 @@ high2 = (balance * (1 + monthlyInterestRate)**12)/12.0 low2 = balance/12 fixedPayment = (high2 + low2)/2 -searching = True def adequatePayment(balance,monthlyInterestRate,fixedPayment): ''' balance - the outstanding balance on the credit card monthlyInterestRate - monthly interest rate as a decimal ''' - month = 1 - while month <= 12: + for month in range(1, 12) balance -= fixedPayment #Update the outstanding balance by removing the payment, balance += monthlyInterestRate*balance #then charging interest on the result. - month += 1 return balance -while searching: +while True: z = adequatePayment(balance,monthlyInterestRate,fixedPayment) if round(z) < 0: high2 = fixedPayment @@ -48,8 +45,8 @@ def adequatePayment(balance,monthlyInterestRate,fixedPayment): low2 = fixedPayment fixedPayment = (high2 + low2)/2 elif round(z) == 0: - searching = False print 'Lowest Payment:',round(fixedPayment,2) + break #check if a character is in a string with recursion @@ -60,9 +57,8 @@ def isIn(char, aStr): returns: True if char is in aStr; False otherwise ''' - low = 0 high = len(aStr)-1 - middle = (high + low)/2 + middle = high/2 if aStr == "" : return False elif len(aStr) == 1: @@ -73,6 +69,3 @@ def isIn(char, aStr): return isIn(char, aStr[:middle-1]) else: return isIn(char, aStr[middle+1:]) - - -