[Tutor] Problem with multiple input
pja
peterjohnanderson at gmail.com
Wed Jul 29 05:30:26 CEST 2009
Hi!
I am trying to teach myself Python (using John Zelle's book "Python
Programming"). 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).
There is a sample script for calculating the sum and difference of two
numbers:
# sum_and_difference.py
def sumDiff(x,y):
total = x + y
diff = x - y
return total, diff
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)
total, diff = sumDiff(number1, number2)
diff = abs(diff)
print("The sum is", total, "and the difference is", diff)
main()
I have commented out the two lines where user input is collected (that
works fine) and tried to replace the two lines with a single input line.
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
this. 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?
Thanks in advance.
Regards,
Peter
--
*Peter Anderson*
There is nothing more difficult to take in hand, more perilous to
conduct, or more uncertain in its success, than to take the lead in the
introduction of a new order of things—Niccolo Machiavelli, /The Prince/,
ch. 6
More information about the Tutor
mailing list