From c2648fa911286349d12dbb11b01ef08a677b8901 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Fri, 12 Feb 2010 02:45:11 +0000 Subject: convert numeric strings to float --- diff --git a/talogo.py b/talogo.py index c238755..9dc2b58 100644 --- a/talogo.py +++ b/talogo.py @@ -68,13 +68,19 @@ class logoerror(Exception): Utility functions """ -def char_to_ord(x, y): - xx = x - yy = y - if type(x) == str or type(x) == unicode and len(x) == 1: - xx = ord(x[0]) - if type(y) == str or type(y) == unicode and len(y) == 1: - yy = ord(y[0]) +def char_to_num(x, y): + try: + xx = float(x) + except ValueError: + xx = x + if type(xx) == str or type(xx) == unicode and len(xx) == 1: + xx = ord(xx[0]) + try: + yy = float(y) + except ValueError: + yy = y + if type(yy) == str or type(yy) == unicode and len(yy) == 1: + yy = ord(yy[0]) return xx, yy def careful_divide(x, y): @@ -96,7 +102,7 @@ def taequal(x, y): typey = True if typex and typey: return x == y - xx, yy = char_to_ord(x, y) + xx, yy = char_to_num(x, y) return xx==yy def taless(x, y): @@ -110,7 +116,7 @@ def taless(x, y): typey = True if typex and typey: return x < y - xx, yy = char_to_ord(x, y) + xx, yy = char_to_num(x, y) return xx>>>>>>>>>>evalinfix %s %s" % (token, str(btoken)) oldcfun, oldarglist = self.cfun, self.arglist self.cfun, self.arglist = token, [firstarg] no_args_check(self) @@ -758,7 +763,7 @@ class LogoCode: self.ireturn() yield True - def prim_bullet(self, list): + def prim_bullet(self, list): # Depreciated block style self.show_bullets(list) self.ireturn() yield True diff --git a/tawindow.py b/tawindow.py index 7b2edee..43e9bfb 100644 --- a/tawindow.py +++ b/tawindow.py @@ -1525,9 +1525,15 @@ class TurtleArtWindow(): b2.name in ['product2', 'minus2', 'sqrt', 'division2', 'random', 'remainder2', 'string']: if b1.name == 'string' and len(b1.values[0]) != 1: - return False + try: + float(b1.values[0]) + except ValueError: + return False elif b2.name == 'string' and len(b2.values[0]) != 1: - return False + try: + float(b2.values[0]) + except ValueError: + return False return True """ -- cgit v0.9.1