try-catch

Rerun code if file doesn't exist is not working

Rerun code if file doesn't exist is not working Question: I have this code to read a file def collect_exp_data(file_name): data = dict() while True: try: with open(file_name, ‘r’) as h: break for line in h: batch, x, y, value = line.split(‘,’) try: if not batch in data: data[batch] = [] data[batch] += [(float(x), float(y), …

Total answers: 3

Unable to Catch Exception and Raise Error in Python

Unable to Catch Exception and Raise Error in Python Question: Suppose that I have a variable initial_cash = ‘a’. I want to check if this is convertible to float else I want to raise error for that I’ve written code below: initial_cash = ‘a’ try: initial_cash = float(initial_cash) except TypeError: raise TypeError("Initial cash amount should …

Total answers: 1

Python Exception Handling – BMI CALCULATOR

Python Exception Handling – BMI CALCULATOR Question: I am coding a BMI calculator in python and wanted to add exception handling. I created two functions 1. a height converter that converts the height to feet or meters depending on the user’s input, and a weight converter that converts the user’s weight to kg or pounds …

Total answers: 2

Stop code execution if a certain value is not present in a column

Stop code execution if a certain value is not present in a column Question: I want only certain values to be filled in a column (from a list A-H) and if the value is not present in that list then the code should thrown an error and stop executing further. res = APAC[‘colA’].isin([‘A’,’B’,’C’,’D’,’E’,’F’,’G’,’H’]) try: for …

Total answers: 1

How can write this in a better way?

How can write this in a better way? Question: Do you have any idea to write this more cleanly ? I am using a module which have several function on it but the syntax is the same like you can see. import browser_cookie3 site = "SOME WEBSITE" cookies = [] try: cookies.append(str(browser_cookie3.chrome(domain_name=site))) except: pass try: …

Total answers: 1

Creating a try and except statement that will try multiple conditions

Creating a try and except statement that will try multiple conditions Question: while True: try: element = driver.find_element(By.XPATH, "//*[contains(@href,’dawson’)]") element.click() break except NoSuchElementException: driver.refresh() time.sleep(3) Above is the try and except block that looks for a word in a Href and if it contains it the element is clicked. I wish to go through multiple …

Total answers: 2

Handle Exception in Python

Handle Exception in Python Question: Code: genders=[] for image in os.listdir(‘Face’): try: gender = int(image.split(‘_’)[1]) except ValueError: pass genders.append(gender) Trying to add int values of string in list. Raises Value error ValueError: invalid literal for int() with base 10: ” so for example : imageName_1 get that one and add to a list. but sometimes …

Total answers: 4

How to identify if Angular toggle-switch toggle is clicked?

How to identify if Angular toggle-switch toggle is clicked? Question: I am trying to define if Angular’s toggle-switch is clicked or not using Selenium and Python tools. When open a new form my input id has class ng-untouched ng-pristine ng-valid. After clicked, it changes to ng-valid ng-dirty ng-touched. But after a third click nothing changes. …

Total answers: 2