nonetype

I'm trying to run the rearrange_name function to rearrange the variable "name" but i keep getting the "NoneType" error

I'm trying to run the rearrange_name function to rearrange the variable "name" but i keep getting the "NoneType" error Question: import re def rearrange_name(name): result= re.search(r’^([w.]*),([w.]*)$’, name) return ‘{} {}’.format(result[2], result[1]) print(result=rearrange_name(‘Lovely Ada’)) The NoneType error message Asked By: reminiscence || Source Answers: The space separating the groups is not being captured by your regex …

Total answers: 1

How to test with parametrize that something is None or not None?

How to test with parametrize that something is None or not None? Question: I have a method that shall return None in some exceptional situations and otherwise something "complicated". In my dummy MWE below (not my real method!) there are two situations (x==0 and x==10) where None is returned. def foo(x: int) -> typing.Optional[float]: if …

Total answers: 2

Python XML Element AttributeError: 'NoneType' object has no attribute 'text'

Python XML Element AttributeError: 'NoneType' object has no attribute 'text' Question: I am trying to parse the following xml file. <xml version="1"> <nodes> 1 -2.50000000E+01 0.00000000E+00 5.00000000E+00 </nodes> </xml> I want to retrieve the values within the element nodes in that file. I have tried the following code: import pandas as pd import xml.etree.ElementTree as …

Total answers: 1

Find if any column in pandas dataframe is NULL and state this in new column

Find if any column in pandas dataframe is NULL and state this in new column Question: I have a dataframe, something like this import pandas as pd dic = {‘animal’:["cat", "dog", "rabbit"], ‘colour’: ["yellow", None, "red"], ‘size’:[None, "large", "small"]} df = pd.DataFrame(dic) animal colour size 0 cat yellow None 1 dog None large 2 rabbit …

Total answers: 2

How to specify a random and None value of a boolean parameter

How to specify a random and None value of a boolean parameter Question: I have some function a which has boolean param b: bool. This parameter can take either a specified value, an empty value, or a random value. def a(b: Optional[bool] = random_bool()) -> None print(b) But if you specify random generation directly in …

Total answers: 1

Replace x by float(x) if and only if it is not None

Replace x by float(x) if and only if it is not None Question: x is either a string or None (for example a query param from a HTTP request: request.query.get(‘x’)). I’d like to transform it into a float if not None. These 3 solutions work: if x is None: y = None else: y = …

Total answers: 2

How do I prevent returning None between desired output?

How do I prevent returning None between desired output? Question: I am trying to return the function with arguments and the functions results in the format of the print statement. The code works except I am getting a "None" between each answer when a test is run. How do I prevent the None from printing? …

Total answers: 1

Function is returning NoneType in Python instead of integer

Function is returning NoneType in Python instead of integer Question: def function(n): if n%4==1: return(n**2) else: n += 1 function(n) # Here for example n=6: if function(6)%2==0: print(function(6)) else: print("Hey!") This is showing the error unsupported operand type(s) for %: ‘NoneType’ and ‘int’. I have tried to convert NoneType with int() but that is telling …

Total answers: 2

Codility OddOccurrencesInArray Problem – Recursion and Python

Codility OddOccurrencesInArray Problem – Recursion and Python Question: I am trying to use recursion to solve the OddOccurrencesInArray Problem in Codility, in which we are given an array with N elements, N is always odd all of the elements of the array except for one has a total even number of occurrences we need to …

Total answers: 6

query database using sqlite3.connect and check for matching results

query database using sqlite3.connect and check for matching results Question: My Discord bot queries a database and checks if there is a matching entry. To check for a matching entry I am using an if statement like here. But somehow my code is not working as expected: def check_db_entry(): message = ‘Some Message.’ user_id : …

Total answers: 2