Are booleans overwritten in python? - python

def space_check(board, position):
return board[position] == ' '
def full_board_check(board):
for i in range(1,10):
if space_check(board, i):
return False
return True
the last line is return True
why not else: return True
if the if statement returned false, won't the last return True overwrite it??

If it was
for i in range(1,10):
if space_check(board, i):
return False
else:
return True
then after the first iteration in the for loop the function would return. This would not lead the the expected behaviour. Currently, you check every space, and not just the first one

Related

How do I logically check for a royal flush in poker?

Code I'm using for card checking
def pair(player_hand):
for x in range(1,len(player_hand)):
if player_hand[x-1][0] == player_hand[x][0]:
return True
return False
def triple(player_hand):
for x in range(2, len(player_hand)):
if player_hand[x][0] == player_hand[x-2][0]:
return True
return False
def flush(player_hand):
if player_hand[0][1] == player_hand[1][1]==player_hand[2][1]==player_hand[3][1]==player_hand[4][1]:
return True
else:
return False
def straight(player_hand):
for x in range(0,len(player_hand)-1):
if(rank(player_hand[x]) + 1 != rank(player_hand[x+1])):
return False
return True
I know how to do a normal flush
def flush(player_hand):
if player_hand[0][1] == player_hand[1][1]==player_hand[2][1]==player_hand[3][1]==player_hand[4][1]:
return True
else:
return False
But how do I check for a specific order of cards for royal flush?

Checking validity of an expression that contains nested parentheses by using stack

I want to use the stack for checking the validity of an expression that contained nested parentheses.
I wrote bellow code but when my input is [{[]}, the code doesn't return anything but I expected to return "left parentheses are more than right ones".
also when my input is (), the response instead of being "balanced" is "mismatched" !!
what is my mistake in the written code?
class Stack:
def __init__(self):
self.items = []
def is_empty(self):
return self.items == []
def push(self, data):
self.items.append(data)
return
def pop(self):
self.items.pop()
def match(left, right):
if left == '[' and right == ']':
return True
elif left == '{' and right == '}':
return True
elif left == '(' and right == ')':
return True
else:
return False
def validity(text):
st = Stack()
for i in text:
if i in '[{(':
st.push(i)
if i in ']})':
if st.is_empty():
print('right parentheses is more than left one')
return False
else:
compare = st.pop()
if not match(compare, i):
print('mismatched')
return False
if st.is_empty():
print('balanced')
else:
print('left parentheses are more than right ones')
for j in range(4):
example = input('plz enter text ')
validity(example)
st.pop() doesn't return anything so compare is always None.
def pop(self):
return self.items.pop()
Should fix it.

nested if python executed in one case but not in another similar condition

Hi please find my code below:
def isIn(char, aStr):
#char: a single character
#aStr: an alphabetized string
#returns: True if char is in aStr; False otherwise
length=len(aStr)
print(length, aStr) #check
if length == 0:
return False
elif length == 1:
if char == aStr:
return True
else:
return False
elif char==aStr[int(length/2)]:
return True
elif char<aStr[int(length/2)]:
print("checked")
aStr=aStr[0:int(length/2)]
isIn(char,aStr)
elif char>aStr[int(length/2)]:
isIn(char,aStr[int(length/2):-1])
if i run with
a='b'
s='b'
v=isIn(a,s)
print(v)
prints out True at the last line
if i run with
a='b'
s='bcd'
v=isIn(a,s)
print(v)
prints out None when it should print True. Any ideas?
you don't need to write a lot of code, you can just do:
def isIn(char, aStr):
return char in aStr
the 'in' keyword will return True if char is in aStr and False otherwise

Codecademy Python “is_prime” exercise in “Practice Makes Perfect”-Is it really iterating?

This function is supposed to return True if a number is prime and False if it's not. The problem is that is_prime(9) returns True.
def is_prime(x):
if x<2:
return False
elif x==2:
return True
elif x==3:
return True
else:
for i in range(2,x):
if x%i==0:
return False
break
else:
return True
Because if you write
for i in range(2,x):
if x%i==0:
return False
break
else:
return True
if x is 9 then 9%2 != 0, so it takes else path and returns True
odd_number % 2 is always = 1
You have to remove the last line and replace it with return True after the for has finished:
def is_prime(x):
if x<2:
return False
elif x==2:
return True
elif x==3: #<-- This one is useless, it will be already checked within the for
return True
else:
for i in range(2,x):
if x%i==0:
return False
break
return True
print is_prime(9)
print is_prime(11)
Your code is improperly indented. The else:return True line should be indented one level less, so that it runs after the for loop terminates. The current code returns True whenver any divisibilty test fails, not when all of the divisibility tests fail.
The function is buggy. The else: inside the loop is returning True the first time it encounters a number that isn't a divisor. That needs to be deleted, and the return True needs to be placed after the loop (outside of it).
Check indentation. The if and else in the for loop.
Understand the meaning of return, the function literally exits when you return.
Corrected code:
def is_prime(x):
if x<2:
return False
elif x==2:
return True
elif x==3:
return True
else:
for i in range(2,x):
if x%i==0:
print "Hello"
return False
break
else:
return True
print is_prime(9)

returning a cube function

I have two functions cube which returns a number cubed and by_three which, if cube is divisible by 3, I need to return cube, else return false. here is what I have so far(below). I keep getting the "Oops, try again. by_three(3) returned True instead of 27 " error, some please help if you know what im doing wrong, or perhaps idiotic!.
def cube(number):
return number**3
def by_three(number):
return number%3==0
if bythree(number):
return cube(number)
else:
return false
Your indentation is all over the place but this will do what you want:
def cube(number):
return number**3
def by_three(number):
return number%3==0
def main(number):
return cube(number) if by_three(number) else False
if by_three(number) is True cube(number) will be called and returned else just False will be returned.
Your code is either unreachable after the return or you have it outside the function where a return will not work. There is also no false in python it is upper case F `
You need a third method.
def cube(number):
return number**3
def by_three(number):
return number%3==0
def whattodo(number):
if by_three(number):
return str(cube(number)) #We must return one type, so we return string for both false and number
else:
return "false"
try:
print int(whattodo(input("Enter a number")) #We are trying to convert the string into an integer
except ValueError: #What if it is not a number? (In this case, it will be a string whose value is "false"
print "Your number is not divisible by three. Screw you"
Try this:
def cube(number):
return number**3
def by_three(number):
if number % 3 == 0:
return cube(number)
else:
return False

Categories

Resources