Python Forum
Questions about while loop - why does one work, and the other variant does not
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Questions about while loop - why does one work, and the other variant does not
#1
Hi,
I am learning Python by studying and doing exercises. Currently I am programming a Tictactoe game as an exercise. it includes the following code snippet:
def GetMove(player):
# Get move for Current Player
    # Clear the Terminal
    Clear()
    # Print Game Title
    Title()
    #Print the Current Board
    PrintCurrBoard()
    #Show the Moves available to the current player
    Choices()
    move=input("\nPlayer {} Please enter the move of your choice: ".format(player))
    response=""
    response=ValidMove(move,player)
    while not response:
        TryAgain=""
        TryAgain=input("That is an Invalid move - Would you like to try again to Continue the Game \
(Y or N)\n\n")
        while TryAgain != "":  # I realize I have to change this statement, it is not what I am working on now.
            GetMove(player)
Now, the above code functions as it is supposed to / correctly, however the <while not response"> was originally written as

while response =="False":
functionally, when written that way, the while loop will only functions if response is actually set equal to false, rather than being set to the return of the ValidateMove function (which is returning True or False, as appropriate). This I do not understand as
while not response:
and

While response == "False":
should be logically equivalent and function in an identical fashion. Help a noob out and explain why my logic seems to be functioning differently than python's.

Wall
Thank You
swgeel
Gribouillis write Aug-29-2024, 07:16 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
False and "False" are two different things. One is bool and the other is str object.
That said, comparing variable for equality or identity to True or False is against PEP8 recommendations.

Quote:Don’t compare boolean values to True or False using ==:

# Correct:
if greeting:
# Wrong:
if greeting == True:
Worse:
# Wrong:
if greeting is True:

Also note
Truth Value Testing

Quote:By default, an object is considered true unless its class defines either a __bool__() method that returns False or a __len__() method that returns zero, when called with the object. Here are most of the built-in objects considered false:
  • constants defined to be false: None and False
  • zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
  • empty sequences and collections: '', (), [], {}, set(), range(0)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Buran: Thank you for instructing a noob and thank you for your answer. I will check out pep 8 and the other suggestion about bbcode. Buran's answer did answer the original question sufficiently
Thank You
swgeek
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  While Loop Does Not Work Properly mactron 4 1,903 Jun-22-2023, 01:04 AM
Last Post: mactron
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 2,982 Dec-18-2021, 02:38 AM
Last Post: knight2000
  How can this for loop work without : ? Pedroski55 1 2,278 Dec-13-2020, 01:19 AM
Last Post: palladium
  Please help my while loop does not work as expected KingKhan248 6 3,739 Sep-28-2020, 09:12 PM
Last Post: deanhystad
  For loop in my __init__ doesn't work as expected Jessy 2 3,047 Nov-18-2019, 10:07 AM
Last Post: buran
Question Why does modifying a list in a for loop not seem to work? umut3806 2 3,185 Jul-22-2019, 08:25 PM
Last Post: umut3806
  Why doesn't my loop work correctly? (problem with a break statement) steckinreinhart619 2 3,938 Jun-11-2019, 10:02 AM
Last Post: steckinreinhart619
  for loop just work one Faruk 1 2,497 Jan-19-2019, 05:34 PM
Last Post: Larz60+
  'Looping' does not work out within a 'for Loop' Placebo 4 4,527 Sep-15-2018, 08:19 PM
Last Post: Placebo
  Having issues getting a loop to work PLESSE HELP ASAP manthus007 1 2,835 Aug-25-2018, 10:44 AM
Last Post: j.crater

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020