language barfs?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Jim Battle
Posts: 6
Joined: Thu May 29, 2014 3:19 am

language barfs?

Post by Jim Battle » Thu May 29, 2014 3:36 am

I just received my micropython borad today and it works -- the little accelerometer demo to drive the LEDs works, I can enter commands at the REPL and all is good so far.

To give it something more to chew on, I wrote this main.py:

Code: Select all

import math
import pyb

def list_primes():
    num_primes = 1
    print("prime # 1 is 2")

    primes = []
    t = 1  # test value

    while(1):
        t += 2
        print("testing",t)
        limit = math.sqrt(t)
        print("limit",limit)
        found = False
        for d in primes:
            if t % d == 0:
                print("... it is divisible by",d)
                found = True
                break  # it's divisible
            if d > limit:
                break # we'll never find it
        if not found:
            num_primes += 1
            print("prime #", num_primes, "is", t)
            primes.append(t)
I reset, open up putty again, then type "primes()" from the REPL to invoke the function declared in main.py. Here is the output:

Code: Select all

>>> list_primes()
prime # 1 is 2
testing 3
limit 1.73205
prime # 2 is 3
testing 5
limit 2.23607
prime # 3 is 5
testing 7
limit 2.64575
prime # 4 is 7
testing 9
limit 3.0
... it is divisible by 3
testing 11
limit 3.31662
prime # 5 is 11
testing 13
limit 3.60555
prime # 6 is 13
testing 15
limit 3.87298
... it is divisible by ... it is divisible by
testing 17
limit 4.12311
... it is divisible by <function>
testing 19
limit 4.3589
testing 21
limit limit
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "0:/main.py", line 19, in primes
TypeError: not all arguments converted during string formatting
This program works and prints thousands of primes in a few seconds on my PC's python interpreter. The fact that it a few iterations work before it goes goofy when testing 15 ("... it is divisible by ... it is divisible by") and 17 ("... it is divisible by <function>"), and then printing the name of the variable "limit" at the end instead of its value.

Does anyone have an idea of what is going wrong here? I've only used python 2.whatever in the past, but other than using the "print" as a function rather than a statement, the micropython python3 dialect is the same code, so it doesn't seem like my error.
Last edited by Jim Battle on Thu May 29, 2014 4:43 am, edited 1 time in total.

Jim Battle
Posts: 6
Joined: Thu May 29, 2014 3:19 am

Re: language barfs?

Post by Jim Battle » Thu May 29, 2014 4:29 am

I just noticed that the function is named "primes" and there is a local array also named "primes." I'm not sure if this is strictly legal, but in case it was causing confusion, I renamed the function to "list_primes". The code produces the same failure as before.

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: language barfs?

Post by pfalcon » Thu May 29, 2014 6:32 pm

I can confirm this issue running on "unix" version. Please submit a bug at https://github.com/micropython/micropython/issues/new .
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

Jim Battle
Posts: 6
Joined: Thu May 29, 2014 3:19 am

Re: language barfs?

Post by Jim Battle » Thu May 29, 2014 7:11 pm


Post Reply