944,043 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 862
  • Python RSS
Nov 5th, 2009
0

Ack... drowning... in... parentheses...

Expand Post »
I swear, those things are like shurikens in these masses...
I'm trying to make a loan calculator in python, as part of an assignment. I'm basing it off of two things.
1: This website, which contains 3 different calculators.
2: The link from the second calculator (my current pain)
I'm going to have a panic attack, I swear... So many parentheses.
I had to manually derive for the Newton-Raphson portion... and of course, now I have so many frekaing parentheses that I can't tell what the heck I'm doing... I mean, sure, I might be a little parenthesis-happy, but I'm new to this and I want to make sure my groupings are all correct...
Python Syntax (Toggle Plain Text)
  1. def findInterestRate():
  2. loanAmount = raw_input("Please enter the loan amount: ")
  3. monthlyPayment = raw_input("Please enter the monthly payment: ")
  4. numberMonths = raw_input("Please enter the number of months: ")
  5. # YAY complications.
  6. finderGuess = (2((numberMonths*monthlyPayment)-loanAmount))/(numberMonths*monthlyPayment)
  7. while zeroFindings != 0:
  8. zeroFindings = ((finderGuess*((1+finderGuess)^numberMonths))/(((1+finderGuess)^numberMonths)-1))-(monthlyPayment/loanAmount)
  9. if ziggy == 1 and zeroFindings != 0:
  10. # now to show off the x~k+1 = x~k - f(x~k)/f`(x~k)
  11. # Yes, the ~ denotes the subscript with the k.
  12. finderGuess = finderGuess - ((((finderGuess*((1+finderGuess)^numberMonths))/(((1+finderGuess)^numberMonths)-1))-
  13. (monthlyPayment/loanAmount))/(((((1+finderGuess)^numberMonths)-1)
  14. (finderguess*(numberMonths*((1+finderGuess)^(numberMonths-1)))+
  15. (1+finderGuess)^((finderGuess*(1+finderGuess)^numberMonths)*
  16. (numberMonths*((1+finderGuess)^(numberMonths-1)))))/
  17. ((((1+finderGuess)^numberMonths)-1)^2))))
  18. ziggy = 1
  19. answer = finderGuess
  20. print "Your interest rate should be: " + str(answer)
Rabblerousers I hate this whole enters thing with parentheses... only way I can see it all in the window...
Takes up a whole freaking lot...
Does little to soothe my pains...
...
Can I get at least one person to help me double-check this, I feel like I'm missing things.
Especially after realizing that I had accidentally hit control+backspace at one point and it erased a small portion at one spot... which means I might have done it multiple times...

And of course, I'm hoping and praying that I get this fixed as I know this will bite my rear in the end if I messed this part up of all things.

Edit: It would be nice if the GUI had color-coded parentheses, to show groupings... or better yet, shading of a similar method...
Last edited by PixelHead777; Nov 5th, 2009 at 9:57 am. Reason: Thought of something slightly random...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
PixelHead777 is offline Offline
14 posts
since Oct 2009
Nov 5th, 2009
1
Re: Ack... drowning... in... parentheses...
Buddy you are not using enough functions, if you need everything in one function(sometimes teachers require this) you can declare functions within a function but it gets pretty messy.
Reputation Points: 35
Solved Threads: 22
Junior Poster
ov3rcl0ck is offline Offline
113 posts
since Sep 2009
Nov 5th, 2009
1
Re: Ack... drowning... in... parentheses...
It would be nice if the GUI had color-coded parentheses, to show groupings... or better yet, shading of a similar method...
Why not use one that does then?

I use Notepad++, but I know that even python-specific IDEs like PyScripter have parenthesis highlighting, etc. In fact, PyScripter will have a red underline on any unmatched parenthesis similar to a "spelling error" in a word processing application.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Nov 5th, 2009
0
Re: Ack... drowning... in... parentheses...
I think you should learn Lisp to get familiar with parenthesis.
Yes, you are using too many of those in your Python code. You can study up in the precedence of mathematical operations to remove a fair number of them.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 5th, 2009
0
Re: Ack... drowning... in... parentheses...
http://xkcd.com/297/
Sorry, i dont usually link things. But this feels appropriate
Reputation Points: 264
Solved Threads: 183
Veteran Poster
Paul Thompson is offline Offline
1,095 posts
since May 2008
Nov 6th, 2009
0
Re: Ack... drowning... in... parentheses...
I would also say that your code suffers from excessively long variable names. Try to keep each code line's total length somewhat less then 70.

Then there is ziggy and a 2 caught between (( etc. etc.

IMHO, the title of this thread should be:
Ack... drowning... in... bad coding style...
Last edited by vegaseat; Nov 6th, 2009 at 5:24 pm.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 6th, 2009
1
Re: Ack... drowning... in... parentheses...
I ran pylint on this code. Here is the result (note that this depends on my personal configuration of pylint, it may give a different result on your system)
Python Syntax (Toggle Plain Text)
  1. $ pylint --disable-msg W0311 bad.py
  2. ************* Module bad
  3. C: 6: Line too long (93/80)
  4. C: 8: Line too long (132/80)
  5. C: 12: Line too long (124/80)
  6. C: 13: Line too long (107/80)
  7. C: 14: Line too long (136/80)
  8. C: 15: Line too long (138/80)
  9. C: 16: Line too long (147/80)
  10. C: 17: Line too long (84/80)
  11. C: 1: Missing docstring
  12. C: 1:findInterestRate: Invalid name "findInterestRate" (should match [a-z_][a-z0-9_]{2,30}$)
  13. C: 1:findInterestRate: Missing docstring
  14. C: 2:findInterestRate: Invalid name "loanAmount" (should match (?:(?:[a-k]|[m-z])|[a-z_][a-z_0-9]{2, 30}))
  15. E: 6:findInterestRate: 2 is not callable
  16. E: 7:findInterestRate: Using variable 'zeroFindings' before assignment
  17. E: 9:findInterestRate: Using variable 'ziggy' before assignment
  18. Exception RuntimeError: 'maximum recursion depth exceeded while calling a Python object' in <type 'exceptions.RuntimeError'> ignored
  19. E: 14:findInterestRate: Undefined variable 'finderguess'
  20.  
  21.  
  22. Report
  23. ======
  24. 12 statements analysed.
  25.  
  26. Duplication
  27. -----------
  28.  
  29. +-------------------------+------+---------+-----------+
  30. | |now |previous |difference |
  31. +=========================+======+=========+===========+
  32. |nb duplicated lines |0 |0 |= |
  33. +-------------------------+------+---------+-----------+
  34. |percent duplicated lines |0.000 |0.000 |= |
  35. +-------------------------+------+---------+-----------+
  36.  
  37.  
  38.  
  39. Raw metrics
  40. -----------
  41.  
  42. +----------+-------+------+---------+-----------+
  43. |type |number |% |previous |difference |
  44. +==========+=======+======+=========+===========+
  45. |code |16 |76.19 |16 |= |
  46. +----------+-------+------+---------+-----------+
  47. |docstring |1 |4.76 |1 |= |
  48. +----------+-------+------+---------+-----------+
  49. |comment |1 |4.76 |1 |= |
  50. +----------+-------+------+---------+-----------+
  51. |empty |3 |14.29 |3 |= |
  52. +----------+-------+------+---------+-----------+
  53.  
  54.  
  55.  
  56. Statistics by type
  57. ------------------
  58.  
  59. +---------+-------+-----------+-----------+------------+---------+
  60. |type |number |old number |difference |%documented |%badname |
  61. +=========+=======+===========+===========+============+=========+
  62. |module |1 |1 |= |0.00 |0.00 |
  63. +---------+-------+-----------+-----------+------------+---------+
  64. |class |0 |0 |= |0.00 |0.00 |
  65. +---------+-------+-----------+-----------+------------+---------+
  66. |method |0 |0 |= |0.00 |0.00 |
  67. +---------+-------+-----------+-----------+------------+---------+
  68. |function |1 |1 |= |0.00 |100.00 |
  69. +---------+-------+-----------+-----------+------------+---------+
  70.  
  71.  
  72.  
  73. Messages by category
  74. --------------------
  75.  
  76. +-----------+-------+---------+-----------+
  77. |type |number |previous |difference |
  78. +===========+=======+=========+===========+
  79. |convention |12 |12 |= |
  80. +-----------+-------+---------+-----------+
  81. |refactor |0 |0 |= |
  82. +-----------+-------+---------+-----------+
  83. |warning |0 |0 |= |
  84. +-----------+-------+---------+-----------+
  85. |error |4 |4 |= |
  86. +-----------+-------+---------+-----------+
  87.  
  88.  
  89.  
  90. Messages
  91. --------
  92.  
  93. +-----------+-----------+
  94. |message id |occurences |
  95. +===========+===========+
  96. |C0301 |8 |
  97. +-----------+-----------+
  98. |E0601 |2 |
  99. +-----------+-----------+
  100. |C0111 |2 |
  101. +-----------+-----------+
  102. |C0103 |2 |
  103. +-----------+-----------+
  104. |E1102 |1 |
  105. +-----------+-----------+
  106. |E0602 |1 |
  107. +-----------+-----------+
  108.  
  109.  
  110.  
  111. Global evaluation
  112. -----------------
  113. Your code has been rated at -16.67/10 (previous run: -16.67/10)
Pylint is a nice tool to help you improve your coding style.
Last edited by Gribouillis; Nov 6th, 2009 at 6:00 pm.
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
Nov 6th, 2009
0
Re: Ack... drowning... in... parentheses...
Thanks for making people aware of the pylint tool. It's been a while since I used it on my own code.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 6th, 2009
0
Re: Ack... drowning... in... parentheses...
Python Syntax (Toggle Plain Text)
  1. finderGuess = finderGuess - ((((finderGuess*((1+finderGuess)^numberMonths))/(((1+finderGuess)^numberMonths)-1))-
  2. (monthlyPayment/loanAmount))/(((((1+finderGuess)^numberMonths)-1)
  3. (finderguess*(numberMonths*((1+finderGuess)^(numberMonths-1)))+
  4. (1+finderGuess)^((finderGuess*(1+finderGuess)^numberMonths)*
  5. (numberMonths*((1+finderGuess)^(numberMonths-1)))))/
  6. ((((1+finderGuess)^numberMonths)-1)^2))))
I hope you are the only one who has to deal with this code. It may help to remember that the compiler can only do one thing at a time, unless you have a multi-core machine and an OS that is multi-core aware, which is very unlikely. So, for most of us there is little between one long calculation and several shorter ones, from the compiler's viewpoint. But there is a big difference in terms of readability.

Note the lack of a mathematical symbol between two lines of code as pointed out below. Also, all of your calculations are done as integers. You may or may not want floats instead.
Python Syntax (Toggle Plain Text)
  1. ## a calculation common to several other calcs
  2. ## increase the guess by one and raise it to the power of the number of months
  3. interim divisor = (1+finderGuess)^numberMonths
  4.  
  5. ## this is the do-hickey which is calculated by
  6. ## subtractng one from the thing-ma-bob and raising it to
  7. ## the power of 2
  8. last_divisor = interim_divisor - 1
  9. last_divisor = last_divisor^2
  10.  
  11. """ note that there is no mathematical symbol between the first and
  12. second lines in this block of code
  13. (((((1+finderGuess)^numberMonths)-1)
  14. (finderguess*(numberMonths*((1+finderGuess)^(numberMonths-1)))+
  15. (1+finderGuess)^((finderGuess*(1+finderGuess)^numberMonths)*
  16. (numberMonths*((1+finderGuess)^(numberMonths-1)))))
  17. """
  18.  
  19. ## so the code becomes something like this, making sure that the number are floats, or are converted in the calculations.
  20. finderGuess = finderGuess - descriptive_name_for_numerator /
  21. descriptive_name_for_first_divisor /
  22. descriptive_name_for_second_divisor /
  23. descriptive_name_for_third_divisor /
  24. last_divisor
Last edited by woooee; Nov 6th, 2009 at 9:40 pm.
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,307 posts
since Dec 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: 1 digit number 2 to 3 digit number 002
Next Thread in Python Forum Timeline: Can Python do this?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC