My Try/Except breaks when inside my function, works alone - try-catch

Okay, first I'm five days in so don't laugh or make it too complicated, hehe.
So in this block of code the try and except works; it prints my error when name doesn't exist.
def list_action(action, name):
if action == "add":
list.append(name)
print(lst_choice)
if action == "remove":
try:
list.remove(name)
print(list)
except ValueError:
print('Error: ' + name + ' is not in the list.')
But in this one it doesn't . I get no traceback so something is working, but I never get the error message.
def list_action(list, action, name):
if list == 1:
if action == 1:
desk.append(name)
print('Done, ' + name +' has been added to list!')
print(desk)
if action == 2:
try:
desk.remove(name)
print('Done, ' + name + ' has been removed from list!')
except ValueError:
print('Name does not exist')
I am totally at a loss of what's causing it. Is it because it's nested once more, because that's the only difference my brain can see at the moment.
Thanks!

Related

How do I check if an object collides with every object in an array?(Picture boxes)

High school student here and I'm pretty rusty on my code. Okay, I have to have an image scroll along, and if it hits an object(in this case both are picture boxes), it resets.
The problem is when it gets to the If statement below, it won't work saying " 'bounds' is not a member of 'system.array' "
If PtbIcon.Bounds.IntersectsWith(objects.Bounds) Then
The error is the Objects.bounds
If PtbIcon.Bounds.IntersectsWith(objects.Bounds) Then
t = t + 1
PtbIcon.Location = New Point(29, 236)
'resets when you die, sets the score
End If
lblScore.Text = "Your Score Equals" & t
End
Why doesn't this work? Why? Is there a simpler way of checking all of this, such as calling a function which checks the bounds individually?
Use Linq.
Dim t As Integer = 0
PtbIcon.All(Function(pb As PictureBox) As Boolean
' Checking goes here with pb
' Return True if you want to go through all of them
End Function)
lblScore.Text = "Your Score Equals" & t

Error: Can't Assign Function Call / Don't want to

It's friday and I'm tired and my brain obvs doesn't want to find this answer. Please help.
I want to assign the value to an array. It works in subsequent lines but not in one particular line, even though syntax seems the same to me? It seems to think I'm calling a function??
for entry in PROJECT:
i = i + 1
#A
if entry.startswith("A") :
ProjectA(i) = entry
#B
elif entry.startswith("B"):
ProjectB(i)= entry
#C
elif entry.startswith("C") :
ProjectC(i) = entry
# and Programme
elif entry.startswith("D") :
ProjectD(i) = entry
I'm told the problem is the last line: "ProjectD(i) = entry". Which to me seems like a replica of "ProjectC(i) = entry"
ProjectA(i) looks like you are calling a function; ProjectA[i] looks like an array element.

Code giving me an ArgumentExeption?

This code is giving me an ArgumentExeption when the correct values are put in both ComboBoxes, executing the code. The code basically just deletes a file and replaces it with a modified version taken from another folder.
Here is the exact text of the error message:
An unhandled exception of type 'System.ArgumentException' occurred in Microsoft.VisualBasic.dll
Additional information: The given file path ends with a directory separator character.
Here's the code:
If ComboBox1.Text = "Nokia" And ComboBox2.Text = "HTC" And My.Computer.FileSystem.FileExists("C:\Users\" + user + "\Documents\Fiddler2\Scripts\CustomRules.js") Then
My.Computer.FileSystem.DeleteFile("C:\Users\" + user + "\Documents\Fiddler2\Scripts\CustomRules.js")
My.Computer.FileSystem.CopyFile("Config\OEM\NokiaHTC.js", destinationFileName:="C:\Users\" + user + "\Documents\Fiddler2\Scripts\")
Else
My.Computer.FileSystem.CopyFile("Config\OEM\NokiaHTC.js", destinationFileName:="C:\Users\" + user + "\Documents\Fiddler2\Scripts\")
End If
The problem is that the destination file path ends with a "\" value. This isn't legal for the CopyFile API. Switch it to include the file name and this should fix the problem
My.Computer.FileSystem.CopyFile( _
"Config\OEM\NokiaHTC.js", _
destinationFileName:="C:\Users\" + user + "\Documents\Fiddler2\Scripts\NokiaHTC.js")

CakePHP encoding problem : storing uppercase S with caron on top, saves in the database but causes errors while processed by cake

So I am working in a site that sores cuneiform tablets info. We use semitic chars for transliteration.
In my script, I create a term list from the translittaration of a tablet.
My problem is that with the Š, my script created two different terms because it thinks there is a space in the word because of the way cake treats the special char.
Exemple :
Partial contents of a tablet :
utu-DIŠ-nu-il2
Terms from the tablet when treated by my script :
utu-DIŠ, -nu-il2
it should be :
utu-DIŠ-nu-il2
When I print the contents of my array in course of treatment of the contents, I see this :
utu-DI� -nu-il2
So this means the uncorrect parsing of the text creates a space that is interpreted in my script as 2 words instead of one.
In the database, the text is fine...
I also get these errors :
Warning (512): SQL Error: 1366: Incorrect string value: '\xC5' for column 'term' at row 1 [CORE\cake\libs\model\datasources\dbo_source.php, line 684]
Query: INSERT INTO terms (term, lft, rght) VALUES ('utu-DI�', 449, 450)
Query: INSERT INTO terms (term, lft, rght) VALUES ('A�', 449, 450)
Query: INSERT INTO terms (term, lft, rght) VALUES ('xDI�', 449, 450)
Anybody knows what I could do to make this work ?
Thanks !
Added info :
$terms=$this->data['Tablet']['translit'];
$terms= str_replace(array('\r\n', '\r', '\n','\n\r','\t'), ' ', $terms);
$terms = trim($terms, chr(173));
print_r($terms);
$terms = preg_replace('/\s+/', ' ', $terms);
$terms = explode(" ", $terms);
$terms=array_map('trim', $terms);
$anti_terms = array('#tablet','1.','2.','3.','4.','5.','6.','7.','7.','9.','10.','11.','12.','13.','14.','15.','16.','17.','18.','19.','20.','Rev.',
'Obv.','#tablet','#obverse','#reverse','C1','C2','C3','C4','C5','C6','C7','C8','C9', '\r', '\n','\r\n', '\t',''. ' ', null, chr(173), 'x', '[x]','[...]' );
foreach($terms as $key => $term) {
if(in_array($term, $anti_terms) || is_numeric($term)) {
unset($terms[$key]);
}
}
If I put my print_r before the preg, the S are good, if I do it after, they display with the black lozenge. So I guess the preg function is the problem !
just found this :
http://www.php.net/manual/fr/function.preg-replace.php#84385
But it seems that
mb_ereg_replace()
causes the same problem as preg_replace() ....
Solutuion :
mb_internal_encoding("UTF-8");
mb_regex_encoding("UTF-8");
$terms = mb_ereg_replace('\s+', ' ', $terms);
and error is gone ... !
mb_internal_encoding("UTF-8");
mb_regex_encoding("UTF-8");
$terms = mb_ereg_replace('\s+', ' ', $terms);

Returning a nicely punctuated string from an object query

In my django app, especially on the admin side, I do a few def's with my models:
def get_flora(self):
return self.flora.all()
def targeted_flora(self):
return u"%s" % (self.get_flora())
whereas flora is a ManyToManyField, however, sometimes ForeignKey fields are used also.
I do this to provide a utility 'get' function for the model, and then the second def is to provide django admin with a a friendlier field name to populate the tabular/list view.
Perhaps a two part question here:
1. Is this a good workflow/method for doing such things, and
2. The resultant string output in admin looks something like:
[<Species: pittosporum>, <Species: pinus radiata>]
Naturally enough, but how to make it look like:
pittosporum & pinus radiata
or, if there were three;
pittosporum, pinus radiata & erharta ercta
Super thanks!
Sounds like you want something like this:
def targeted_flora(self):
names= [f.name for f in self.get_flora()] # or however you get a floras name
if len(names) == 1:
return names[0]
else:
return ', '.join(names[:-1]) + ' & ' + names[-1]
This works btw:
def punctuated_object_list(objects, field):
if field:
field_list = [getattr(f, field) for f in objects]
else:
field_list = [str(f) for f in objects]
if len(field_list) > 0:
if len(field_list) == 1:
return field_list[0]
else:
return ', '.join(field_list[:-1]) + ' & ' + field_list[-1]
else:
return u''

Resources