I'm using a while loop in some code. This is the third one in this given program, all of them are formatted the same, yet for some reason one specific line is giving me syntax error. I have tried copying and pasting it directly from another portion of my while loop, and nothing seems to be rectifying it. I'm absolutely baffled. Perhaps another set of eyes can see what I'm missing?
close the parenthesis on the previous line
Related
Is there any shortcut to automatically indent marked lines in the editor? For example, in MATLAB there is the CTRL+I shortcut.
Matlab's syntax can match the opening closing statements of if,while, for etc. by looking for end statements.
In Python these are ambiguous and defined as the nested indentetation. Hence this cannot be reliably implemented as you cannot decide whether the succeeding if block belongs the the current for loop or it is the next block, if not indented properly.
If indented properly then Forzaa's answer is the answer otherwise the code is useless anyway and needs to be debugged.
First I will just reconfirm what has been said above, that python is ambiguous about what should be the correct indent. Unfortunately as coming from Matlab I also like Ctrl-I.
Though just check on how Tab and Shift-Tab work in practice, they did a little better than I expected. When I ended up having 2 tabs too much after rearranging code, one Shift-tab brought it back to proper position.
I have the following python code inside a large loop
arr_a[indx]*arr_b[arr_c[indx],]
and with one running, an exception occurred and it said index out of range, but there are two possibilities(indx is out of range for predictErr, or arr_c[indx]), how do I know which part goes wrong?
This problem also extend to some general case that when one write several operations in one line and when things goes wrong, it is hard to tell which part causes this, and note that the above mentioned expression is inside a large loop, which means one can not simply start a debug mode and find that out.
Add a print statement for each segment that might be to blame to see which one fails:
print arr_a[indx]
print arr_c[indx]
arr_a[indx]*arr_b[arr_c[indx],]
So I have been messing around with python with openCV the last couple of weeks and it has worked perfectly. My problem is when I took two desperate code script's and put them together it wouldn't compile. (I know Duh python white space (I fixed all of it,"I think")) It threw an error in the terminal with a line number. At this time I was using text editor in Ubuntu so I threw it into Geany to figure out what line. When I got there I couldn't pick out what the error was all of the indentation was that of the original code and it fitted with that of the rest of the loop it was nested in. So I tried to compile it in Geany and it didn't throw errors at all. I find this extremely weird because Geany is only an editor and it relies on outside compilers to compile the code. I am assuming that the terminal is using the same compilers too (though I know it must not now). I though maybe it has something to do with the library openCV because I am not including it in Geany. So I changed the variable name that was throwing things. After that it still threw the same error so I came here confused.
The error message I am getting is
http://imgur.com/1otMyeZ
My code is at
http://pastebin.com/HYKjnyyc
The part that is giving error is here
http://pastebin.com/6TyXs3uc
Your problem still goes back to the white space you were working on. Everything (except line 297 to the end) below:
# THE DEMO PROGRAM STARTS HERE
is indented, including your three global variables, which makes them appear to the compiler as part of the class Target, which takes the variables out of the global scope, so they are not defined. Remove the unnecessary indentation in that area.
EDIT:
Looking back at your code again, You may also have some indentation problems from the
# Function Definitions
at line 88 and down. From the way the functions are indented they appear to be part of the class, but from the way they are called they appear not to be.
You would find writing in python a lot less frustrating if you use a more standard approach to your indentation/white-space handling. Read this PEP for some best practices in that regard.
I've been trying to submit a solution in Python to a problem in Spoj, but I keep getting an NZEC runtime error.
Is it possible to find out which line the error is occurring at?
No, not directly: SPOJ won't tell you - it won't even tell you which non-zero exit code you got :-(
The slow way around this is to submit your program many times, changing it each time to exit at a later line. For example, call sys.exit() after your first line. If you don't get a NZEC complaint, you know your first line wasn't the cause. Then move sys.exit() down a line and try again. Etc. It can be a real PITA.
Note: you can do a form of binary search this way to find the offending line much faster.
Edit 2:
Solved, see my answer waaaaaaay below.
Edit:
After banging my head a few times, I almost did it.
Here's my (not cleaned up, you can tell I was troubleshooting a bunch of stuff) code:
http://pastebin.com/ve4Qkj2K
And here's the problem: It works sometimes and other times not so much. For example, it will work perfectly with some files, then leave one of the longest codes instead of the shortest one, and for others it will delete maybe 2 out of 5 duplicates, leaving 3 behind. If it just performed reliably, I might be able to fix it, but I don't understand the seemingly random behavior. Any ideas?
Original Post:
Just so you know, I'm just beginning with python, and I'm using python 3.3
So here's my problem:
Let's say I have a folder with about 5,000 files in it. Some of these files have very similar names, but different contents and possible different extensions. After a readable name, there is a code, always with a "(" or a "[" (no quotes) before it. The name and code are separated by a space. For example:
something (TZA).blah
something [TZZ].another
hello (YTYRRFEW).extension
something (YJTR).another_ext
I'm trying to only get one of the something's.something, and delete the others. Another fact which may be important is that there are usually more than one code, such as "something (THTG) (FTGRR) [GTGEES!#!].yet_another_random_extension", all separated by spaces. Although it doesn't matter 100%, it would be best to save the one with the least codes.
I made some (very very short) code to get a list of all files:
import glob
files=[]
files=glob.glob("*")
but after this I'm pretty much lost. Any help would be appreciated, even if it's just pointing me in the right direction!
I would suggest creating separate array of bare file names and check the condition if any element exists in any other place by taking array with all indices excluding the current checked in loop iteration.
The
if str_fragment in name
condition simply finds any string fragment in any string-type name. It can be useful as well.
I got it! The version I ended up with works (99%) perfectly. Although it needs to make multiply passes, reading,analyzing, and deleting over 2 thousand files took about 2 seconds on my pitiful, slow notebook. My final version is here:
http://pastebin.com/i7SE1mh6
The only tiny bug is that if the final item in the list has a duplicate, it will leave it there (and no more than 2). That's very simple to manually correct so I didn't bother to fix it (ain't nobody got time fo that and all).
Hope sometime in the future this could actually help somebody other than me.
I didn't get too many answers here, but it WAS a pretty unusual problem, so thanks anyway. See ya.