All seems to be working pretty well, but I run into an odd issue when using the ord() command. The following script, causes an error
Code: Select all
# main.py -- put your code here!
import math
import os
x = ord(os.urandom(1))%100
z = 0
b = 0
n = 0
leds = [pyb.LED(i) for i in range(1,5)]
while x != int(z):
b=b+1
z = input("Guess My Number: ")
if int(z) < x: print("Higher!")
if int(z) > x: print("Lower!")
n = (n + 1) % 4
leds[n].toggle()
print("Correct! " + str(b) + " tries.")
File "main.py", line 5, in <module>
TypeError: ord() expected a character, but string of length 0 found
So it seems to have to due with the ord() command, I can recreate this issue by repeatedly sending this command via REPL...sometimes it works sometimes it has the issue. os.urandom() seems to work just fine all the time, but wrapped in ord() seems to fail randomly.
>>> os.urandom(1)
b'C'
>>>
>>> os.urandom(1)
b'\x99'
>>>
>>> os.urandom(1)
b' '
>>> os.urandom(1)
b'\xad'
>>> os.urandom(1)
b'1'
>>> ord(os.urandom(1))
83
>>> ord(os.urandom(1))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ord() expected a character, but string of length 0 found
>>> ord(os.urandom(1))
32
>>> ord(os.urandom(1))
28
>>> ord(os.urandom(1))
1
>>> ord(os.urandom(1))
84
>>> ord(os.urandom(1))
68
>>> ord(os.urandom(1))
104
>>> ord(os.urandom(1))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ord() expected a character, but string of length 0 found
>>> ord(os.urandom(1))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ord() expected a character, but string of length 0 found
>>> ord(os.urandom(1))
35
>>> ord(os.urandom(1))
31
>>> ord(os.urandom(1))
98
>>> ord(os.urandom(1))
40
>>> ord(os.urandom(1))
122
>>> ord(os.urandom(1))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ord() expected a character, but string of length 0 found
>>> ord(os.urandom(1))
84
>>> ord(os.urandom(1))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ord() expected a character, but string of length 0 found
>>> ord(os.urandom(1))
209
>>> ord(os.urandom(1))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ord() expected a character, but string of length 0 found
>>> ord(os.urandom(1))
239
>>> ord(os.urandom(1))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ord() expected a character, but string of length 0 found
>>> ord(os.urandom(1))
Has anyone seen this before? Or do you think this might be an issue with my port?
Thanks for any help.