except

How to monitor internet connectivity using infitnite while loop Python?

How to monitor internet connectivity using infitnite while loop Python? Question: I am using an infinite while loop to perform specific function if internet is available. And to print an statement if internet is not available. Following is the code. import requests,time while True: print("HI") try: requests.get(‘https://www.google.com/’).status_code print(‘online’) except: print(‘offline’) The code works on first …

Total answers: 1

How do I create a function/method out of this code and then call it to the main()?

How do I create a function/method out of this code and then call it to the main()? Question: enter code hereThis is the original code that I wrote: while True: user_input = (input(">>",)) try: user_input = int(user_input) except ValueError: pass if user_input in range(1, len(something): break I want to put in a method: `get_user_answer(n: int, …

Total answers: 3

'except' in try statement (inside while loop) isn't working

'except' in try statement (inside while loop) isn't working Question: I’ve written a program which repeatedly reads numbers until the user enters "done". If the user enters anything other than a number, detect their mistake using try and except and print an error message and skip to the next number. The problem is the code …

Total answers: 2

How to retry after sql connection failed in python?

How to retry after sql connection failed in python? Question: What’s the right way to retry when connection/write to a DB fails in python? I’m trying to use this code and it works until i restart my sql server and python tries to connect to it i get "retry after 30 sec" for 10 times …

Total answers: 1

What is wrong with using a bare 'except'?

What is wrong with using a bare 'except'? Question: I tried making a function to check if an image is displayed on the screen using PyAutoGui and came up with this: def check_image_on_screen(image): try: pyautogui.locateCenterOnScreen(image) return True except: return False And it works fine, but PyCharm tells me I shouldn’t leave except bare. What is …

Total answers: 2

Python: Multiple try except blocks in one?

Python: Multiple try except blocks in one? Question: Is there a neat way to have multiply commands in the try block so that it basically tries every single line without stopping as soon as one command yields an error? Basically I want to replace this: try: command1 except: pass try: command2 except: pass try: command3 …

Total answers: 5

Safe method to get value of nested dictionary

Safe method to get value of nested dictionary Question: I have a nested dictionary. Is there only one way to get values out safely? try: example_dict[‘key1’][‘key2’] except KeyError: pass Or maybe python has a method like get() for nested dictionary ? Asked By: Arti || Source Answers: You could use get twice: example_dict.get(‘key1’, {}).get(‘key2’) This …

Total answers: 33

Multiple try codes in one block

Multiple try codes in one block Question: I have a problem with my code in the try block. To make it easy this is my code: try: code a code b #if b fails, it should ignore, and go to c. code c #if c fails, go to d code d except: pass Is something …

Total answers: 9

Catch KeyError in Python

Catch KeyError in Python Question: If I run the code: connection = manager.connect(“I2Cx”) The program crashes and reports a KeyError because I2Cx doesn’t exist (it should be I2C). But if I do: try: connection = manager.connect(“I2Cx”) except Exception, e: print e It doesn’t print anything for e. I would like to be able to print …

Total answers: 7

Invalid syntax (SyntaxError) in except handler when using comma

Invalid syntax (SyntaxError) in except handler when using comma Question: I have this code: @app.route(‘/login/’, methods=[‘GET’, ‘POST’]) def login(): error = None if request.method == ‘POST’: session[‘username’] = request.form[‘username’] session[‘password’] = request.form[‘password’] try: # use reddit_api’s login r.login(user=session[‘username’], password=session[‘password’]) except InvalidUserPass, e: error = ‘Incorrect username or password. ‘ if not error: subreddits = r.user.get_my_reddits(limit=25) …

Total answers: 5