EDIT: SOLVED--SOURCE CODE HERE: http://matthewdowney20.blogspot.com/2011/09/source-code-for-roku-remote-hack.html
thanks in advance for reading and possibly answering this. So I have a slice of code that looks like this (the commands Down() Select() and Up() are all predefined):
def c1(row):
row_down = row
row_up = row
while row_down > '1':
Down()
row_down = row_down - 1
time.sleep(250)
Select()
time.sleep(.250)
while row_up > '1':
Up()
row_up = row_up - 1
time.sleep(250)
So when I run this with either c1('3') or c1(3) (not jut 3, any number does this) it stops responding, no error or anything, but it executes the first Down() command, and it doesnt seem to get past the row_down = row_down - 1 . So i figure maybe it is stuck on time.sleep(.250), because it isnt executing the Select(), so if i remove time.sleep(.250) from the code i get an error like this:
Traceback (most recent call last):
File "test.py", line 338, in <module>
c1('3')
File "test.py", line 206, in c1
row_down = row_down - 1
TypeError: unsupported operand type(s) for -: 'str' and 'int'
this code snippet is part of a larger program designed for controlling the roku player from a computer, and so far everything has worked but this, which is to automate the typing in the search field, so that you do not have to continually scroll until you find a letter and select. c1(row) would be column 1 row x, if any of you would like the source code for the program over all, i would be happy to send it out. Anyway thanks for listening.
Perhaps you meant
while row_down > 1:
(note 1 is written without quotes). If so, call c1 with c1(3) not c1('3').
Also, in CPython (version 2, but not version 3) integers are comparable to strings, but the answer is not what you might expect:
3 > '1'
# False
When comparing any integer to any string, the integer is always less than string because (believe it or not!) i (as in integer) comes before s (as in string) in the alphabet.
As TokenMacGuy has already pointed out, addition of integers with strings raises a TypeError:
'3' - 1
# TypeError: unsupported operand type(s) for +: 'int' and 'str'
This might explain the error you are seeing when calling c1('3').
>>> x = raw_input('enter a number: ')
enter a number: 5
>>> x
'5'
>>> type(x)
<type 'str'>
>>> x + 5
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>> type(int(x))
<type 'int'>
>>> int(x) + 5
10
>>>
(if you're using python3, use input instead of raw_input)
I suspect that the loop is running with out error because you can subtract from a character to change it: "b - 1 = a". (read edit) It also doesn't error is because, like Marcelo Cantos said in his comment, the first time.sleep is for 250 seconds, not .250 seconds. The error when you remove the time.sleep might be coming up when you subtract past the the ASCII character range since it runs through the loop a lot quicker without the time.sleep.
I hope that helps!
Edit: Actually, I think what I said works in C or something. In python, it doesn't work. The other stuff I said might shed some light though!
Related
so i was coding some problems in python in this page
https://www.codewars.com/kata/airport-arrivals-slash-departures-number-1/train/python
the code work fine on my computer but when i update it, i came across this bug.
note that its python 3.4.3
def flap_display(lines, rotors):
baseString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ?!##&()|<>.:=-+*/0123456789"
res = []
baseLen = len(baseString)
lineLen = len(lines)
sLen = len(rotors)
carrier = 0
for item in range(0 , sLen):
if (item < lineLen):
carrier =carrier + rotors[item]
tmp = baseString.index(lines[item])
tmp = tmp + carrier
tmp = tmp % baseLen
res.append( baseString[tmp] )
resS = ''.join(res)
return resS
print (flap_display("CAT", [1,13,27]))
all the website gave me is this:
Traceback:
in
in flap_display
TypeError: unsupported operand type(s) for +: 'int' and 'list'
Now i want to know if my code is incorrect or its just the site being buggy.
Problem is solved! Thank to mr.kuro
sum requires an iterable: a sequence of items, such as a list. You gave it a single integer. If you want to add up all the integers in rotors, you can do that outside of a loop, with
carrier = sum(rotors)
More to your code, just add up the items you wanted:
carrier = sum(rotors[:lineLen])
This adds the first lineLen elements of rotors, allowing you to get rid of that pesky if statement.
Can you adapt the rest of the loop logic to take proper advantage of that?
Thr traceback should be like below:
Traceback (most recent call last):
File "test1.py", line 17, in
print (flap_display("CAT", [1,13,27]))
File "test1.py", line 10, in flap_display
carrier =carrier + sum(rotors[item])
TypeError: 'int' object is not iterable
And, as the traceback says, in line
carrier =carrier + sum(rotors[item])
rotors[item] will apparently be an int, so you can't call sum on it, hence the Error.
Replace the above line with:
carrier = carrier + rotors[item]
Or, just skip the loop, and do:
carrier = sum(rotors)
It should be okay now.
def main():
spiral = open('spiral.txt', 'r') # open input text file
dim = spiral.readline() # read first line of text
print(dim)
if (dim % 2 == 0): # check to see if even
dim += 1 # make odd
I know this is probably very obvious but I can't figure out what is going on. I am reading a file that simply has one number and checking to see if it is even. I know it is being read correctly because it prints out 10 when I call it to print dim. But then it says:
TypeError: not all arguments converted during string formatting
for the line in which I am testing to see if dim is even. I'm sure it's basic but I can't figure it out.
The readline method of file objects always returns a string; it will not convert the number into an integer for you. You need to do this explicitly:
dim = int(spiral.readline())
Otherwise, dim will be a string and doing dim % 2 will cause Python to try to perform string formatting with 2 as an argument:
>>> '10' % 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting
>>>
Also, doing print(dim) outputed 10 instead of '10' because print automatically removes the apostrophes when printing:
>>> print('10')
10
>>>
I made a program to calculate pi (just as a bit of fun, I don't want to use the pi that's built in) in Python 3.3.2. It works fine - it gets the correct value of pi up to 7 decimal places. However, once it gets to this point, the program continues but the values do not change. I have tried using decimal.Decimal, but it does not work - it comes up with the following error:
TypeError: unsupported operand type(s) for +: 'decimal.Decimal' and 'float'
This is my working code (using floats instead of decimals.) I have only included the actual algorithm :
pi=float(3)
a=2
b=0
c=float(0)
while 1==1:
b=a*(a+1)*(a+2)
c=4/b
pi=pi+c
print(str(pi))
a=a+2
b=a*(a+1)*(a+2)
c=4/b
pi=pi-c
print(str(pi))
a=a+2
And this is the result:
http://i.imgur.com/y0qgBMB.png (sorry, I don't have enough reputation to post images directly)
Please note that the program actually stopped working much earlier than this, I just left it there for a while.
I know that the algorithm that I am using is capable of finding pi to many more decimal places, so can someone please tell me why it is not working, and give me any possible solutions?
As you might notice from the simplicity of my code that I am not too good at python - only the basic knowledge that I have been taught at school. Simple solutions would be much appreciated!
EDIT
This is the non-working code with Decimal, and Jasper's suggestion in it.
import decimal
pi=decimal.Decimal(3)
a=2
b=0
c=decimal.Decimal(0)
while True:
b=a*(a+1)*(a+2)
c=4/b
pi=pi+c
print(str(pi))
a=a+2
b=a*(a+1)*(a+2)
c=4/b
pi=pi-c
print(str(pi))
a=a+2
This is the error:
Traceback (most recent call last):
File "C:\Users...\fastpi2.py", line 9, in
pi=pi+c TypeError: unsupported operand type(s) for +: 'decimal.Decimal' and 'float'
Your code fails because the division of two integers results in a float (in Python 3):
>>> from decimal import Decimal
>>> pi = Decimal(3)
>>> a=2
>>> b=a * (a+1) * (a+2)
>>> c=4/b
>>> type(c)
<class 'float'>
>>> pi = pi + c
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'Decimal' and 'float'
The following works:
>>> a = Decimal(2)
>>> one = Decimal(1)
>>> b = a * (a+one) * (a+one+one)
>>> c = Decimal(4) / b
>>> type(c)
<class 'decimal.Decimal'>
>>> pi = pi + c
>>> pi
Decimal('3.166666666666666666666666667')
import sys
def func():
T = int(next(sys.stdin))
for i in range(0,T):
N = int(next(sys.stdin))
print (N)
func()
Here I am taking input T for for loop and iterating over T it gives Runtime error time: 0.1 memory: 10088 signal:-1 again-again . I have tried using sys.stdin.readline() it also giving same error .
I looked at your code at http://ideone.com/8U5zTQ . at the code itself looks fine, but your input can't be processed.
Because it is:
5 24 2
which will be the string:
"5 24 2"
this is not nearly an int, even if you try to cast it. So you could transform it to the a list with:
inputlist = next(sys.stdin[:-2]).split(" ")
to get the integers in a list that you are putting in one line. The loop over that.
After that the code would still be in loop because it want 2 integers more but at least you get some output.
Since I am not totally shure what you try to achieve, you could now iterate over that list and print your inputs:
inputlist = next(sys.stdin[:-2]).split(" ")
for i in inputlist
print(i)
Another solution would be, you just put one number per line in, that would work also
so instead of
5 24 2
you put in
5
24
2
Further Advice
on Ideone you also have an Error Traceback at the bottom auf the page:
Traceback (most recent call last):
File "./prog.py", line 8, in <module>
File "./prog.py", line 3, in func
ValueError: invalid literal for int() with base 10: '1 5 24 2\n'
which showed you that it can't handle your input
I run
import sys
print "x \tx^3\tx^3+x^3\t(x+1)^3\tcube+cube=cube+1"
for i in range(sys.argv[2]): // mistake here
cube=i*i*i
cube2=cube+cube
cube3=(i+1)*(i+1)*(i+1)
truth=(cube2==cube3)
print i, "\t", cube, "\t", cube + cube, "\t", cube3, "\t", truth
I get
Traceback (most recent call last):
File "cube.py", line 5, in <module>
for i in range(sys.argv[2]):
IndexError: list index out of range
How can you use command line parameter as follows in the code?
Example of the use
python cube.py 100
It should give
x x^3 x^3+x^3 (x+1)^3 cube+cube=cube+1
0 0 0 1 False
1 1 2 8 False
2 8 16 27 False
--- cut ---
97 912673 1825346 941192 False
98 941192 1882384 970299 False
99 970299 1940598 1000000 False
Use:
sys.argv[1]
also note that arguments are always strings, and range expects an integer.
So the correct code would be:
for i in range(int(sys.argv[1])):
You want int(sys.argv[1]) not 2.
Ideally you would check the length of sys.argv first and print a useful error message if the user doesn't provide the proper arguments.
Edit: See http://www.faqs.org/docs/diveintopython/kgp_commandline.html
Here are some tips on how you can often solve this type of problem yourself:
Read what the error message is telling you: "list index out of range".
What list? Two choices (1) the list returned by range (2) sys.argv
In this case, it can't be (1); it's impossible to get that error out of
for i in range(some_integer) ... but you may not know that, so in general, if there are multiple choices within a line for the source of an error, and you can't see which is the cause, split the line into two or more statements:
num_things = sys.argv[2]
for i in range(num_things):
and run the code again.
By now we know that sys.argv is the list. What index? Must be 2. How come that's out of range? Knowledge-based answer: Because Python counts list indexes from 0. Experiment-based answer: Insert this line before the failing line:
print list(enumerate(sys.argv))
So you need to change the [2] to [1]. Then you will get another error, because in range(n) the n must be an integer, not a string ... and you can work through this new problem in a similar fashion -- extra tip: look up range() in the docs.
I'd like to suggest having a look at Python's argparse module, which is a giant improvement in parsing commandline parameters - it can also do the conversion to int for you including type-checking and error-reporting / generation of help messages.
Its sys.argv[1] instead of 2. You also want to makes sure that you convert that to an integer if you're doing math with it.
so instead of
for i in range(sys.argv[2]):
you want
for i in range(int(sys.argv[1])):