Python function, returning False and None - python

I have this function
def postData():
data = doPOst()
if data.result == "success":
return data
if data.result == "fail":
return false
Then i based on return i have to do something like
if ret is None:
pass # 1
if ret is False:
pass # 2
else:
normal
I want to know that will None and False get mixed

If you check for None and False explicitly, then no, they will not get combined.
if ret is None: # entered only on None
pass
elif ret == False: # entered only on False or 0
pass
else:
pass
If you just checked for truthy/falsey values, they would get combined:
if ret:
pass
else: # entered on None or False
pass

Related

Finding if there is a space in a list(return boolean value)

Looking for some help on a tick tac toe exercise:
I have the bellow:
test_board = ['#','X','O','X','O','X','O','','O','X']
def space_check(board,position):
free_space = ""
if free_space in board[position]:
return True
else:
return False
When running the function I cannot see the False return, only True:
space_check(test_board,7)
Output: True
space_check(test_board,9)
Output: True
If free_space in board[position]
You are search a "" in a string not in a list of string,so the results always true because "" always exist in any string.
You just Use equal operation == instead of in.
Change your function with this:
def space_check(board,position):
free_space = ""
return free_space == board[position]
test_board = ['#','X','O','X','O','X','O','','O','X']
space_check(test_board, 7)
Output : True
space_check(test_board, 9)
Output: False

Return empty string as True in given string

def first_and_last(message):
if (message[0] == message[3]):
return True
elif (message[0] != message[3]):
return False
print(first_and_last("else"))
print(first_and_last("tree"))
print(first_and_last(""))
I want to return True if string is empty, True if 1st and last letter of string match and False otherwise.
How can I get a True result for an empty string?
You wrote that you want to compare the first character to the last,
so you have to use [-1] and not [3]. Otherwise you are comparing
the first and the fourth characters.
You can use if not message to check if it's an empty string
Since you are returning, you don't need to check if they do not match.
def first_and_last(message):
if not message:
return True
return message[0] == message[-1]
As noted in the comments this can be pushed a bit more into a single line:
def first_and_last(message):
return not message or message[0] == message[-1]
You can use if not
def first_and_last(message):
if not message:
return False
else: return True
print(first_and_last("else")) #True
print(first_and_last("tree")) #True
print(first_and_last("")) #False
# defines, name and establishes the arguments for the function
def first_and_last(message):
# "if not message" checks if the parameter is empty
# message[0] gets the first character in the parameter
# message[-1] gets the last character in the parameter
# "==" checks if the first and last character are the same in the
# parameter
# the "or" comparator checks if the statements on either side or
# the comparator is true
if not message or message[0] == message[-1]:
# if either one or both of the above statements are true the
# function returns True
return True
# otherwise returns False
return False
You can use if not message: to check if the message variable is empty.
You could, for example, do as follow :
def first_and_last(message):
if not message:
return True
elif (message[0] == message[3]):
return True
elif (message[0] != message[3]):
return False
print(first_and_last("else"))
print(first_and_last("tree"))
print(first_and_last(""))
You can check if the string is empty with bool(message). And check the last item with message[-1]:
def first_and_last(message):
if not message:
return True
elif (message[0] == message[-1]):
return True
else:
return False
print(first_and_last("else")) # Returns True
print(first_and_last("tree")) # Returns False
print(first_and_last("")) # Returns True
Use:
if not message:
This will return true if the string is empty:
def first_and_last(message):
if not message:
return True
if (message[0] == message[3]):
return True
elif (message[0] != message[3]):
return False
print(first_and_last("else"))
print(first_and_last("tree"))
print(first_and_last(""))
def first_and_last(message):
if not message:
return True
elif (message[0] == message[-1]):
return True
elif (message[0] != message[-1]):
return False
print(first_and_last("else"))
print(first_and_last("tree"))
print(first_and_last(""))
def first_and_last(message):
if message=="" or message[0]==message[-1]:
return True
return False
Here, message == "" checks for empty string and after "or" it checks for first and last letter of word.
Just to make it more readable since you are learning and haven't used "if not"
def first_and_last(message):
if message == "":
return True
elif message[0] == message[-1]:
return True
else:
return False
print(first_and_last("else"))
print(first_and_last("tree"))
print(first_and_last(""))
output would be below
True
False
True
def first_and_last(message):
if message == "" or message[0] == message[-1]:
return True
else:
return False
print(first_and_last("else"))
print(first_and_last("tree"))`enter code here`
print(first_and_last(""))
# could it work like this as well? if not why? thanks :)
def first_and_last(message):
while bool(message) == True:
first_letter = str(message[0])
last_letter = str(message[-1])
if first_letter == last_letter:
return True
else :
return False
return True
print(first_and_last("else"))
print(first_and_last("tree"))
print(first_and_last(""))
# Output (True, False, True)

Blank List, Return False

I'm looking to write a function definition named has_evens that takes in sequence of numbers and returns True if there are any even numbers in the sequence and returns False otherwise.
My code is correct, except when it receives something empty, like "([])". I need to account for that. Here's my code:
def has_evens(s):
for num in s:
if num % 2 == 0:
return True
elif num % 2 != 0:
return False
if ([]):
return False
The final part is a desperate attempt to account for blank lists. More formally, it needs to pass this assertion:
assert has_evens([]) == False
You should only return True when an even is found:
def has_evens(s):
for num in s:
if num % 2 == 0:
return True
return False
Python has an any function to make this simpler:
def has_evens(s):
return any(num % 2 == 0 for num in s)
I answered my own question. I just needed to un-indent my lines
def has_evens(s):
for num in s:
if num % 2 == 0:
return True
else:
return False

Function improperly marks CSV rows as corrupt

I have a CSV file with data in it that should be either a float or an integer, but if its not, the row is marked as corrupt.
I have this if statement that checks several of the columns in each row to see if they are not empty, and if they are a float or integer. Currently, the program marks every row in the CSV file as corrupt. How do I fix this?
def check_if_number(num) :
for i in num:
if i == "." or i == "":
pass
else:
try:
float(i)
except (ValueError, TypeError):
return False
return True
def check_if_empty(item) :
if item == "" :
return True
else:
return False
if (check_if_empty(row[5]) == False
and check_if_number(row[5]) == False
or check_if_empty(row[6]) == False
and check_if_number(row[6]) == False
or check_if_empty(row[8]) == False
and check_if_number(row[8]) == False
or check_if_empty(row[9]) == False
and check_if_number(row[9]) == False
or check_if_empty(row[10]) == False
and check_if_number(row[10]) == False):
corrupt = True
I think your second function is returning True when you want it to return False?
def check_if_empty(item) :
if item == "" :
return False
else:
return True
EDIT:
I have simplified and attached an updated working code that yields what you are looking for just to help substantiate my thought.
def check_if_number(num):
float(num)
for i in num:
if i == "." or i == "":
pass
else:
try:
float(i)
except (ValueError, TypeError):
return False
return True
def check_if_empty(item):
if item == "" :
return False
else:
return True
if check_if_empty('.4') == True and check_if_number('.4') == True:
corrupt = False
else:
corrupt = True
print(corrupt)
... False

python return none instead of True/False

I have two programs for searching using binary search in Python
Program 1:
def bin(alist,x):
if len(alist)==0:
return False
else:
mid=len(alist)//2
if (alist[mid]==x):
return True
else:
if alist[mid] < x:
#print(alist[mid+1:],x)
bin(alist[mid+1:],x)
else:
#print(alist[:mid],x)
bin(alist[:mid],x)
print (bin([2,3,5,8,9],8))
print (bin([2,3,5,8,9],7))
Program output:
None
None
Program 2:
def bin(alist,x):
if len(alist)==0:
return False
else:
mid=len(alist)//2
if (alist[mid]==x):
return True
else:
if alist[mid]<x:
return bin(alist[mid+1:],x)
else:
return bin(alist[:mid],x)
print(bin([1,5,7,8,9],10))
print(bin([1,4,5,8,9],8))
Program output:
False
True
Why is it so?
In your program 1, only when the list is empty or the value you are searching for in the middle of the list, it would returns you boolean value, that's because you explicitly say return if len(alist)==0 and return True when it meets if (alist[mid]==x):, you have to do the same for the rest conditions as well
def bin(alist,x):
if len(alist)==0:
return False
else:
mid=len(alist)//2
if (alist[mid]==x):
return True
else:
if alist[mid] < x:
#print(alist[mid+1:],x)
bin(alist[mid+1:],x) # -------> return
else:
#print(alist[:mid],x)
bin(alist[:mid],x) # -------> return
When you invoke your bin() method recursively and expect a boolean value, you have to add return in the above highlighted lines.

Categories