pja wrote:
Hi!

I am trying to teach myself how to program in Python using Zelle's "Python Programming: An Introduction to Computer Science" (a very good text). At the same time I have decided to start with Python 3 (3.1.1). That means that I have to convert Zelle's example code to Python 3 (which generally I cope with).

I'm hoping that somebody can help with what's probably a very simple problem. There is a quadratic equation example involving multiple user inputs from the one "input" statement. The code works fine with Python 2.5 but when I convert it to Python 3 I get error messages. The code looks like:

05 import math
06
07 def main():
08 print("This program finds the real solutions to a quadratic\n")
09
10 a, b, c = input("Please enter the coefficients (a, b, c): ")
11
12 '''
13 a = int(input("Please enter the first coefficient: "))
14 b = int(input("Please enter the second coefficient: "))
15 c = int(input("Please enter the third coefficient: "))
16 '''
17
18 discrim = b * b - 4 * a * c
19 ...

25 main()

Lines 08 to 12 are my Python 3 working solution but line 06 does not work in Python 3. When it runs it produces:

Please enter the coefficients (a, b, c): 1,2,3
Traceback (most recent call last):
File "C:\Program Files\Wing IDE 101 3.2\src\debug\tserver\_sandbox.py", line 25, in <module> File "C:\Program Files\Wing IDE 101 3.2\src\debug\tserver\_sandbox.py", line 10, in main
builtins.ValueError: too many values to unpack
>>>

Clearly the problem lies in the input statement. If I comment out line 10 and remove the comments at lines 12 and 16 then the program runs perfectly. However, I feel this is a clumsy solution.

Could somebody please guide me on the correct use of "input" for multiple values.

Regards,
Peter
You will need to split your input.

no_input = True
while no_input:
   in = input("Please enter the coefficients (a, b, c): ")
   try:
       a, b, c = map(int, in.split(','))
       no_input = False
   except ValueError:
print("Please enter your input in a comma-seperated format with no space. Only 3 numbers allowed")

That will loop allowing input until the person enters 3 numbers seperated purely by commas in for input.

--
Kind Regards,
Christian Witts
Business Intelligence

C o m p u s c a n | Confidence in Credit

Telephone: +27 21 888 6000
National Cell Centre: 0861 51 41 31
Fax: +27 21 413 2424
E-mail: cwi...@compuscan.co.za

NOTE:  This e-mail (including attachments )is subject to the disclaimer 
published at: http://www.compuscan.co.za/live/content.php?Item_ID=494.
If you cannot access the disclaimer, request it from 
email.disclai...@compuscan.co.za or 0861 514131.

National Credit Regulator Credit Bureau Registration No. NCRCB6

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to