Welcome, guest | Sign In | My Account | Store | Cart
def main():
    while True:
        try:
            string = raw_input('What numbers should I average? ')
            words = string.split()
            numbers = map(float, words)
            average = sum(numbers) / len(numbers)
            print 'The average is', average
            raw_input('Press enter to quit.\n')
            return
        except:
            print 'ERROR: I can only take numbers!'

if __name__ == '__main__':
    main()

Diff to Previous Revision

--- revision 1 2005-10-02 18:35:19
+++ revision 2 2012-07-11 12:27:59
@@ -1,13 +1,15 @@
-loop = True
-while loop:
-    try:
-        numbers = raw_input('What numbers should I average? ')
-        numbers = numbers.split(' ')
-        total = 0.0
-        for number in numbers:
-            total += int(number)
-        print 'The average is ' + str(total / len(numbers)) + '.'
-        raw_input('Press enter to quit.')
-        loop = False
-    except:
-        print 'Error: I can only take numbers!'
+def main():
+    while True:
+        try:
+            string = raw_input('What numbers should I average? ')
+            words = string.split()
+            numbers = map(float, words)
+            average = sum(numbers) / len(numbers)
+            print 'The average is', average
+            raw_input('Press enter to quit.\n')
+            return
+        except:
+            print 'ERROR: I can only take numbers!'
+
+if __name__ == '__main__':
+    main()

History