"pja" <peterjohnander...@gmail.com> wrote

One major problem is that the book is written for Python 2.x and I am using Python 3.1 (and don't want to switch back).

Very few books/tutorials exist yet for v3. But you seem to be
coping with the differences adequately. My tutorial for v3 is
almost half-way complete, you might find it useful as a secondary
resource:

http://www.alan-g.me.uk/l2p/


def main():
    # Introduction
print("This program calculates the sum and difference of two numbers.")
    # number1 = float(input("Enter the first number: "))
    # number2 = float(input("Enter the second number: "))
    number1, number2 = (input("Please enter two numbers: ").split())
    number1, number2 = float(number1), float(number2)

The example code works but I don't think it is either optimum or elegant. Could someone help me with the "correct" Python way of doing

That loooks OK although its a little bit unusual to request two input
values on one line - just because of the difficulty of parsing the result.
Its hard enough to get users to input correct values one at a time!
Never mind formatting two values... as you discovered:

Also the working code only woks with input numbers that are separated by a space. How would I do this with numbers that are separated by a comma?

However you can pass a string to split() that will split the line on
any of the characters in the string. So in your case you could
request a split using spaces and commas - and I suggest adding
semi colons and tabs too?

number1, number2 = (input("Please enter two numbers: ").split(" ,;\t"))


HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to