richpl / PyBasic

Simple interactive BASIC interpreter written in Python
GNU General Public License v3.0
165 stars 45 forks source link

Can't input float number #18

Closed hengying closed 3 years ago

hengying commented 3 years ago

When I try this program:

10 INPUT A
20 PRINT A

I input 2.34, and the error is displayed: String input provided to a numeric variable

I'm not sure if my program is correct.

I make a simple modification: basicparser.py line: 451

                elif not left.endswith('$'):
                    try:
                        self.__symbol_table[left] = int(right)
                    except ValueError:
                        try:
                            self.__symbol_table[left] = float(right)
                        except ValueError:
                            raise ValueError('String input provided to a numeric variable ' +
                                         'in line ' + str(self.__line_number))

Is there any better solution?

richpl commented 3 years ago

Wow, thought I'd tested all of that stuff. I'll take a look. Thanks for the feedback.

richpl commented 3 years ago

I don't know if this is the most elegant solution, but I uploaded the following fix:

                    try:
                        if '.' in right:
                            self.__symbol_table[left] = float(right)
                        else:
                            self.__symbol_table[left] = int(right)

                    except ValueError:
                        raise ValueError('Non-numeric input provided to a numeric variable ' +
                                         'in line ' + str(self.__line_number))

The input code may end up being overhauled anyway in response to the other current issue, but this should work for now.

JiFish commented 3 years ago

Suggest doing it the way I did the VAL function. The advantage is that 1.0 would be stored as an INT

numeric = float(value)
if numeric.is_integer():
    numeric = int(numeric)
self.__symbol_table[left] = numeric

But this can wait for #19