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

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 9
Reputation: PixelHead777 is an unknown quantity at this point 
Solved Threads: 0
PixelHead777 PixelHead777 is offline Offline
Newbie Poster

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

 
0
  #1
27 Days Ago
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...
  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; 27 Days Ago at 9:57 am. Reason: Thought of something slightly random...
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 106
Reputation: ov3rcl0ck is an unknown quantity at this point 
Solved Threads: 11
ov3rcl0ck ov3rcl0ck is offline Offline
Junior Poster
 
1
  #2
27 Days Ago
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.
NOTE: sudo doesn't apply to real life situations.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,046
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 264
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is
 
1
  #3
27 Days Ago
Originally Posted by PixelHead777 View Post
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.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,025
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 932
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #4
27 Days Ago
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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 907
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 145
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is online now Online
previously paulthom12345
 
0
  #5
26 Days Ago
http://xkcd.com/297/
Sorry, i dont usually link things. But this feels appropriate
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,025
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 932
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #6
25 Days Ago
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; 25 Days Ago at 5:24 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 926
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 216
Gribouillis's Avatar
Gribouillis Gribouillis is online now Online
Posting Shark
 
1
  #7
25 Days Ago
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)
  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; 25 Days Ago at 6:00 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,025
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 932
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #8
25 Days Ago
Thanks for making people aware of the pylint tool. It's been a while since I used it on my own code.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,017
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 286
woooee woooee is online now Online
Veteran Poster
 
0
  #9
25 Days Ago
  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.
  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; 25 Days Ago at 9:40 pm.
Linux counter #99383
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC