language barfs?
Posted: 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:
I reset, open up putty again, then type "primes()" from the REPL to invoke the function declared in main.py. Here is the output:
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.
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)
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
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.