Why does this program hang? Google App Engine and Python -
this program supposed compute numeric palindrome, when data input via web form. did run on localhost environment using python , web.py framework. since then, ported google app engine, using webapp2 framework, can launch on public server. when refactored code new framework, got off in lines of code dealing computation. hoping can solve this.
below class handles getting input browser. i've tested , seems input fine. sends compute_palintip
, supposed solve palindrome (final amount). hanging here.
class result(webapp2.requesthandler): def post(self): form = self.request.get_all('entries', default_value = none) if form[0]: if form[1]: orig_dollar_amount = cgi.escape(form[0]) tip_percent = cgi.escape(form[1]) final_amount = compute_palintip(float(orig_dollar_amount), float(tip_percent)) actual_tip_percent = final_amount - orig_dollar_amount actual_tip_percent = actual_tip_percent*100/orig_dollar_amount
this compute_palintip
code , helper function:
def compute_palintip(orig_dollar_amount, tip_percent): orig_amount_cents = orig_dollar_amount*100 final_amount = orig_amount_cents*(1.0 + tip_percent/100.0) while(is_palindrome(final_amount) == false): final_amount += 1 final_amount = final_amount/100 final_amount = round(final_amount, 2) return final_amount def is_palindrome(num): num_str = str(num) num_str_len = len(num_str) in range(num_str_len): if num_str[i] != num_str[num_str_len-i-1]: return false return true
a few notes:
- i had convert input floats in order send them
compute_palintip
function. - couldn't convert ints because not allow decimal places (cents needed these inputs related restaurant dollar amounts).
- i have use float type i'm pretty sure, because neither strings nor ints able operate formulas floats. python wants them same.
- however, seems math wonky since floats , ints got mixed up, , not terminating.
the math written colleague, whereas writing ui type stuff, why i'm not able debug @ point, hoping else can spot solution i'm not seeing. thanks.
perhaps can set logging see flow of program. when dealing in floats, consistent in arithmetic, , use decimals after each number: orig_dollar_amount*100.0
final_amount/100.0
final_amount += 1.0
actual_tip_percent*100.0/orig_dollar_amount
also, failed cast orig_dollar_amount
float
Comments
Post a Comment