Skip to content

Slightly more pythonic conditional #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions bisectionsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,35 @@

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

monthlyInterestRate = annualInterestRate/12.0
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
Expand All @@ -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

Expand All @@ -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:
Expand All @@ -73,6 +69,3 @@ def isIn(char, aStr):
return isIn(char, aStr[:middle-1])
else:
return isIn(char, aStr[middle+1:])