Basic Tic-Tac-Toe game. I have everything running except I am having trouble with the check_for_winner function. No matter what I do I can't get the code to exit the function once a = True. Without the while loop or with. How can I get the code to stop checking every single possible instance once a winner has been detected?
I've tried setting a == False, I've tried it without the while statement which gets rid of the looping but doesn't get it to exit out when the condition is met.
def check_for_winner():
a = False
while a is False:
for x in range(3):
if print(grid[x] == grid[x+3] and grid[x+3] == grid[x+6]):
a = True
return a
for x in range(0,7,3):
if print(grid[x] == grid[x+1] and grid[x+1] == grid[x+2]):
a = True
return a
if print((grid[0] == grid[4] and grid[4] == grid[8]) or (grid[2] ==
grid[4] and grid[4] == grid[6])):
a = True
return a
It should exit the while loop once one the conditions is met but it continues to loop through. Instead of exiting then printing who one it prints True the False six times, repeating.
The problem is all the unnecessary calls to print(). They're all returning None which causes the if checks to all be equivalent to if None:. Get rid of them.
def check_for_winner():
a = False
while a is False:
for x in range(3):
if grid[x] == grid[x+3] and grid[x+3] == grid[x+6]:
a = True
return a
for x in range(0,7,3):
if grid[x] == grid[x+1] and grid[x+1] == grid[x+2]:
a = True
return a
if (grid[0] == grid[4] and grid[4] == grid[8]) or (grid[2] ==
grid[4] and grid[4] == grid[6]):
a = True
return a
return False
Then you can get rid of the unnecessary a variable and the while loop.
def check_for_winner():
for x in range(3):
if grid[x] == grid[x+3] and grid[x+3] == grid[x+6]:
return True
for x in range(0,7,3):
if grid[x] == grid[x+1] and grid[x+1] == grid[x+2]:
return True
if (grid[0] == grid[4] and grid[4] == grid[8]) or (grid[2] ==
grid[4] and grid[4] == grid[6]):
return True
return False
You can also use comparison chaining to replace foo == bar and bar == baz with foo == bar == baz.
def check_for_winner():
for x in range(3):
if grid[x] == grid[x+3] == grid[x+6]:
return True
for x in range(0,7,3):
if grid[x] == grid[x+1] == grid[x+2]:
return True
if grid[0] == grid[4] == grid[8] or grid[2] == grid[4] == grid[6]:
return True
return False
Related
Python: Why don't I get a result in the terminal
def is_even(number):
if number % 2 == 0:
return True
return False
is_even(10)
You need to print the output of the function. You also need to create the function.
def is_even(num):
if num % 2 == 0:
return True
else:
return False
print(is_even(10)) #True
print(is_even(7)) #False
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?
In the below program, even though all the if conditions are matching, it returns true just once. How do i make it return true and print as many times as the conditions match?
lotto_numbers = [1,1,1]
fireball_number = 1
user_input1 = user_input2 = user_input3 = 1
def fbcheck():
if lotto_numbers == [user_input1,user_input2,fireball_number]:
return True
elif lotto_numbers == [fireball_number, user_input2, user_input3]:
return True
elif lotto_numbers == [user_input1, fireball_number, user_input3]:
return True
else:
return False
if (fbcheck() == True):
print ('you won')
You can use all:
def fbcheck():
user_data = [user_input1,user_input2,fireball_number]
lotto_numbers = [1,1,1]
print([a==b for a, b in zip(lotto_numbers, user_data)])
return all(a==b for a, b in zip(lotto_numbers, user_data))
print(fbcheck())
Output:
[True, True, True]
True
I have a problem. I want to create a form in python. If something is wrong, I want an alert-window (showwarning()). Otherwise it should write 'TRUE' into the command line.
The problem is that I get every time a alert-window. It does not care if the form is filled out correctly or wrong.
Can somebody help me with this problem?
code:
""" Variables """
inputError_1 = bool(0)
inputError_2 = bool(0)
inputError_3 = bool(0)
valueCheck = bool(0)
""" Check-Button """
def Check():
if len(nameOne.get()) == 0:
inputError_1 == TRUE
elif len(nameTwo.get()) == 0:
inputError_2 == TRUE
elif len(comment.get(INSERT)) == 0:
inputError_3 == TRUE
else:
valueCheck = bool(1)
if inputError_1 == FALSE or inputError_2 == FALSE or inputError_3 == FALSE:
showwarning()
else:
print'TRUE'
I think that you can do this in a simpler way:
def check():
if len(nameOne.get()) == 0 or len(nameTwo.get()) == 0 or len(comment.get(INSERT)) == 0:
showwarning()
else:
print 'True'
check()
I have set a very simple example here:
def sourcecode(a,b,c,alternatief=False):
volume_a, volume_b = 0, 0
while volume_a != c or volume_b != c:
print(volume_a,volume_b)
volume_a += 1
volume_b += 2
if volume_a == c:
return volume_a
elif volume_b == c:
return volume_b
print(sourcecode(7,3,5))
Of course this will be an infinite loop as always 1 of the two will never reach c.
But what I try to figure out is how to write my while statement to stop if one of two conditions are met.
It is a relatively simple tweak:
while volume_a != c and volume_b != c:
^
This requires both to be True to continue, so stops looping if either one becomes False.
With or:
True or True == True
True or False == True
False or True == True
False or False == False # need both to match to break out
With and:
True and True == True
True and False == False # break out here
False and True == False # or here
False and False == False
You need to use and, not or. When one of the condition is False, the loop will quit:
while volume_a != c and volume_b != c:
print(volume_a,volume_b)
volume_a += 1
volume_b += 2
If you're using or, it will stop only when both conditions are False. By using and, the loop will continue only if both conditions are True.
Hope this helps!
others response are good but you could simply use an infinite loop and returning from it
def sourcecode(a,b,c,alternatief=False):
volume_a, volume_b = 0, 0
while True:
if volume_a == c:
return volume_a
if volume_b == c:
return volume_b
print(volume_a,volume_b)
volume_a += 1
volume_b += 2
print(sourcecode(7,3,5))