try:
float(self.entry_weight.get()) and int(self.entry_height.get())
self.get_bears()
except:
messagebox.showinfo("Number Error","Your weight and height must be a number!")
Hello,
This try/except statement doesn't seem to be working, when I input numbers into the entry boxes, and click the associated button, I only get the message box saying that my inputs are not numbers.
I have used print statements to make sure I am testing the right entry boxes and i am. When i test the type i get back as expected but surely if these string inputs are numbers, the try except statement should work. Any help would be great thanks.
Your try/except block covers too much territory. In addition to problems with input, it catches everything that could go wrong with get_bears. Additionally, and short-circuits so the second check is never made if weight is 0. You can use two try/except blocks to cover everything
try:
# validate input
float(self.entry_weight.get())
int(self.entry_height.get())
except ValueError:
messagebox.showinfo("Number Error","Your weight and height must be a number!")
return
try:
self.get_bears()
except:
messagebox.showinfo("Unhandled Error","Unknown error in program")
# log it somewhere
import traceback
traceback.print_exc()
# likely want to exit() because program is highly busted
Related
I've run into a bug where the program seems to think that my input into an input statement isn't within the parameters, when it clearly is.
This is the code i'm having trouble with:
time.sleep(1.5)
print()
print()
print("You have chosen", horsechoice)
time.sleep(1)
print("How much do you want to bet?")
while True:
try:
moneyinput = int(input())
racebegin(moneyinput)
break
except:
print("Not a valid amount of money.")
continue
Even when I input an integer, it still states that I didn't input a valid amount of money.
If you only want to check the input validity, you should wrap the try/except only around the int(input()) call, not racebegin(). Your code is probably catching an error in racebegin().
It's also a good idea to narrow down the type of error you're catching with except:. Doing that might have prevented the problem in the original code, unless racebegin() was also raising ValueError.
while True:
try:
moneyinput = int(input())
break
except ValueError:
print("Not a valid amount of money.")
racebegin(moneyinput)
Alright, so I've got a try and except block watching out for exceptions.
This is all normal but when you've got a loop you can't stop the code usually, as there's no specific bit saying "Don't catch KeyboardInterrupt errors" or something alike
Is there anything that let's try and except blocks exclude specific errors? (or an alternative)
My code
while 1:
if ques == 'n':
try:
newweight = int(input('Please enter a number'))
exit()
except:
print('Please enter a valid number')
Now this will create an infinite loop as the "exit()" will not happen as it will get raised an as exception, and it will thus create an infinite loop. Is there any way for this to not happen?
To prevent a user entering a non-integer value, you want to catch a TypeError so you can change except: to except ValueError: and it will then catch any invalid inputs. That should cover you sufficiently.
I'm in trouble about how to end a 'try' loop, which is occurred since I have the 'try', here is the code:
import time
class exe_loc:
mem = ''
lib = ''
main = ''
def wizard():
while True:
try:
temp_me = input('Please specify the full directory of the memory, usually it will be a folder called "mem"> ' )
if temp_me is True:
exe_loc.mem = temp_me
time.sleep(1)
else:
print('Error value! Please run this configurator again!')
sys.exit()
temp_lib = input('Please specify the full directory of the library, usually it will be a folder called "lib"> ')
if temp_lib is True:
exe_loc.lib = temp_lib
time.sleep(1)
else:
print('Invalid value! Please run this configurator again!')
sys.exit()
temp_main = input('Please specify the full main executable directory, usually it will be app main directory> ')
if temp_main is True:
exe_loc.main = temp_main
time.sleep(1)
I tried end it by using break, pass, and I even leaves it empty what I get is Unexpected EOF while parsing, I searched online and they said it is caused when the code blocks were not completed. Please show me if any of my code is wrong, thanks.
Btw, I'm using python 3 and I don't know how to be more specific for this question, kindly ask me if you did not understand. Sorry for my poor english.
EDIT: Solved by removing the try because I'm not using it, but I still wanna know how to end a try loop properly, thanks.
Your problem isn't the break, it's the overall, high-level shape of your try clause.
A try requires either an except or a finally block. You have neither, which means your try clause is never actually complete. So python keeps looking for the next bit until it reaches EOF (End Of File), at which point it complains.
The python docs explain in more detail, but basically you need either:
try:
do_stuff_here()
finally:
do_cleanup_here() # always runs, even if the above raises an exception
or
try:
do_stuff_here()
except SomeException:
handle_exception_here() # if do_stuff_here raised a SomeException
(You can also have both the except and finally.) If you don't need either the cleanup or the exception handling, that's even easier: just get rid of the try altogether, and have the block go directly under that while True.
Finally, as a terminology thing: try is not a loop. A loop is a bit of code that gets executed multiple times -- it loops. The try gets executed once. It's a "clause," not a "loop."
You have to also 'catch' the exception with the except statement, otherwise the try has no use.
So if you do something like:
try:
# some code here
except Exception:
# What to do if specified error is encountered
This way if anywhere in your try block an exception is raised it will not break your code, but it will be catched by your except.
i'm in a situation where i take the user input and do some math operations.. The user selects an item from a combobox, and inputs a number in the line edit next to it. When he clicks ok, it returns (comboboxselectedvalue)+(lineeditvalue), it works, most of the time, my problem is, when the user leaves the lineedit in blank, it returns an error:
ValueError: invalid literal for int() with base 10: ''
cb1 = self.lineEdit.text()
zb1=self.comboBox.currentText()
az1 = int(cb1)
print(zb1+az1)
How can i set the value of line edits to 0 when nothing is typed in and left in blank? I have 20 line edits and 20 combo boxes..
Your code is going to raise an exception whenever the user enters anything that's not a number—including nothing at all.
The nicest way to handle this is by validating the entry (and pre-filling it with something valid, like 0), so they can't click OK with something invalid in the box.
But if you want to handle this in the "engine" code instead of in the GUI, you can; it just won't be as nice a user experience.
It sounds like what you want to do is treat an empty box as 0. What about a non-empty but non-numeric entry? Should that also be treated as 0? If so, this is easy; the exception already catches exactly what you want to catch, so you just need to handle it.
try:
az1 = int(cb1)
except ValueError:
az1 = 0
If, on the other hand, you want to treat non-numeric entries different from empty, it's a bit more complicated; the exception lumps them both in together, so you'd need an if test either instead of or in addition to the exception. If, say, you wanted non-numeric text to abort the program as it currently does, but an empty box to mean 0, you could write:
try:
az1 = int(cb1)
except ValueError:
if not az1:
az1 = 0
else:
raise
Or, more briefly:
az1 = int(cb1) if cb1 else 0
Currently using the wxPython framework and my code looks like this:
Event bind:
self.frequency_grid.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.on_cell_changed)
Function that handles changed cells:
def on_cell_changed(self, event):
self.current_grid = event.GetEventObject()
try:
new_value= self.get_cell_value()
if new_value < 0:
raise AttributeError
#allow the cell to update
except AttributeError:
event.Veto()
wx.MessageBox(_("Positive values only."), "", wx.OK|wx.ICON_WARNING)
except:
wx.MessageBox(_("Invalid value for cell."), "", wx.OK|wx.ICON_WARNING)
event.Veto()
The function get_cell_value() reads the value from the current cell and converts it to an integer simply by using int(). If the user enters a character like 'a' into the cell, obviously this function fails and the exception is raised. In this case the messagebox comes out telling the user the cell has an invalid value. This is what I call the automatically caused exception, and the final exception block is executed.
In the case of negative values, I manually raise an AttributeError (just wanted to see something different from ValueError which is what happens when user inputs characters).
In this case however, the wxPython sends the EVT_GRID_CELL_CHANGE event twice, so there must be something different about manually raised exceptions.
I've separately raised a ticket about the duplicated events at http://trac.wxwidgets.org/ticket/16333 but just trying to understand how the first scenario doesn't make wxPython send 2 events compared to the second scenario.
Don't use except: if you really want to catch any possible error(even system memory or anything else non-related).
To make your code look better, I'd suggest to cast value to int with try...except block, then check for negative values.
try:
value = int(text)
except:
return MessageBox('Enter digit')
if value < 0:
return MessageBox('Enter positive digit')
There is no difference in the Exceptions, but you are processing them in a different way, at a different place in the code.
I suspect it's the veto() call that causes the extra event. But why not validate the input in one place only? i.e have your get_cell_value() function should make sure it's an integer >0.
Also, this structure is both a recipe for circular events, and a painfully annoying UI. People should be able to make a typo and go back and correct it without an annoying dialog popping up.
Maybe provide an indicator of a problem, like making the cell background red, but only go the dialog route once the user tries to move on to the next step.
After a bit more tracing, I found that creating the MessageBox causes the EVT_GRID_CELL_CHANGING event to occur, which then leads to the EVT_GRID_CELL_CHANGED event to occur, which is why I saw duplicated events.
The reason why I did not see duplicated events during the entry of a character was because a Veto() was called in the event handler for the EVT_GRID_CELL_CHANGING if an int() conversion was invalid because my handler for that event gets the grid input and tries to convert it.
In conclusion, there is no difference in Python exception handling, but however, a better wxPython demo should be implemented to prevent the duplicated message box during the demo and show other users how to better use the grid mechanism.