Debug me please

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

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

Debug me please

 
0
  #1
Oct 24th, 2009
Alright, this is how my newest program looks like:

  1. import math
  2.  
  3. def main():
  4. print('This program finds the real solutions to a quadratic')
  5. print
  6.  
  7. s = float(input('Please enter the coefficients (a,b,c): '))
  8. a, b, c = s.split(',')
  9.  
  10.  
  11. discRoot = float(math.sqrt(b * b - 4 * a * c))
  12. root1 = (-b + discRoot) / (2*a)
  13. root2 = (-b - discRoot) / (2*a)
  14.  
  15. print
  16. print('The solutions are:', root1, ('and'), root2)
  17.  
  18. main()

and i get this error msg:
in main
in main
s = float(input('Please enter the coefficients (a,b,c): '))
ValueError: invalid literal for float(): 2, 3, -2



Help ._.
Last edited by simpatar; Oct 24th, 2009 at 9:53 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,109
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: 943
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #2
Oct 24th, 2009
I assume you are using Python3?

When you are splitting the input string you still get strings, so each coefficients (a,b,c) has to be converted to a float before you apply it to your formula.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 19
Reputation: simpatar is an unknown quantity at this point 
Solved Threads: 0
simpatar simpatar is offline Offline
Newbie Poster
 
0
  #3
Oct 24th, 2009
Correct, I use python3.


I deleted the float infront of the input since it was causing the program to bug. But shouldnt I be able to tell python to make all variables that i put in the input to be floats? if not,
how do I make them float nicely?
Last edited by simpatar; Oct 24th, 2009 at 10:28 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,067
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: 268
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is
 
0
  #4
Oct 24th, 2009
Originally Posted by simpatar View Post
Correct, I use python3.


I deleted the float infront of the input since it was causing the program to bug. But shouldnt I be able to tell python to make all variables that i put in the input to be floats? if not,
how do I make them float nicely?
Basically you have to iterate over your "split" coefficients and then perform float conversion on each one independently. When you split them they return a list, which you'll be doing your iteration on. Here's the steps broken down:
  1. >>> my_coeffs = input('Enter coefficients (a,b,c): ')
  2. Enter coefficients (a,b,c): 3, 5.5, 4.25
  3. >>> coeff_list = my_coeffs.split(',')
  4. >>> coeff_list
  5. ['3', ' 5.5', ' 4.25']
  6. >>> # Create an empty list to store our float values
  7. >>> float_list = []
  8. >>> # Iterate over our string coefficients
  9. >>> for each in coeff_list:
  10. ... # turn each one into a float
  11. ... conv_coeff = float(each)
  12. ... # and finally store it
  13. ... float_list.append(conv_coeff)
  14. ...
  15. >>> float_list
  16. [3.0, 5.5, 4.25]
  17. >>>
But thankfully in python you can make it look a little more beautiful than the above using list comprehension:
  1. >>> my_coeffs = raw_input('Enter coefficients (a,b,c): ')
  2. Enter coefficients (a,b,c): 2, 4.5, 5.25
  3. >>> float_list = [float(each) for each in my_coeffs.split(',')]
  4. >>> float_list
  5. [2.0, 4.5, 5.25]
  6. >>>
List comprehension allows us to create a list from another iterable while performing some action (and there's even room for logic). This is a powerful tool that you would probably benefit from learning.

Here's the documentation about it
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,109
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: 943
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #5
Oct 24th, 2009
Just modify your code, also take care of negative numbers in your square root ...
  1. import math
  2.  
  3. def main():
  4. print('This program finds the real solutions to a quadratic')
  5. print()
  6.  
  7. s = input('Please enter the coefficients (a,b,c): ')
  8. a, b, c = s.split(',')
  9. a = float(a)
  10. b = float(b)
  11. c = float(c)
  12.  
  13. disc = b * b - 4 * a * c
  14. if disc < 0:
  15. print("root will give imaginary number")
  16. return
  17. discRoot = math.sqrt(disc)
  18. root1 = (-b + discRoot) / (2*a)
  19. root2 = (-b - discRoot) / (2*a)
  20.  
  21. print()
  22. print('The solutions are:', root1, ('and'), root2)
  23.  
  24. main()
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC