Python - While Statements Syntax? Logic?

Hi,
I’ve included my code below, I started off at Ex14.py and at the end was curious about if statements which then lead me to the while function and I ended up experimenting a little bit. Sharing this because although I could get the program to run the way I wanted to in the end, there were a few things that I could not figure out, pointers are welcome.

I could not get this code to work with several other things I had previously written on line 26 which were:

while likes not in negative_decision or not in positive_decision:

  • Here I would expect that the program would check to see if the variable likes was not in either of the lists defined in the negative_decision or positive_decision variable. Instead I get a Syntax Error with the little arrow hovering over in, as highlighted in bold above.

while likes not in (negative_decision, positive_decision):

  • Then I thought, well since it’s a Syntax error perhaps this could do the trick. If the value of variable likes is not contained in either of the variables within the parenthesis then the program will do what I want it to do. I was wrong. I was stuck in a loop no matter what the user input was. Then I thought, Maybe what I’m actually saying is that the variable likes cannot be equal to a value within both lists contained in the variables within parenthesis. I added a common value under both variables to test this theory but this was also wrong. Worth nothing that although the error is now gone, the program is still not doing what I want it to.

while likes not in negative_decision and not in positive_decision:

  • Here I thought, well it must be the missing and that is causing the Syntax error. Tried it out still the same Syntax Error.

Finally I decided i’d solve the problem by doing what you can find in the code that I posted at the end. I created a new variable called possible_decision and included everything from negative_decision and positive_decision in one big list. This solved my problem, but I can’t help think that there is probably an easier way.
I then thought, let me try

possible_decision = negative_decision, positive_decision

It didn’t work. I defined the variables, but again it didn’t work. I figured this could be because I’m checking to see if my input is = all of the values contained within negative_decision at once instead of being equal to one of them and similarly with positive_decision too.
Although it felt stupid I tried placing every single value contained in both lists by spacing the strings out, by including the comma’s or by inserting a single valid value from both lists, all to no avail.

I was hoping someone could comment on the logic/what I’m thinking so that I can better understand if I’m thinking correctly and figure out how this is pieced together. Thank you!!

Below is the code that works, using the workaround I came up with using the possible_decisions variable.

from sys import argv

script, user_name, weapon, = argv   #names and sets number of arguments to run scrypt

prompt = '\u2623' #sets the variable prompt to Unicode Character u2623. (Graphical representation Doesn't print  in PowerShell by default, see https://stackoverflow.com/questions/57131654/using-utf-8-encoding-chcp-65001-in-command-prompt-windows-powershell-window/57134096#57134096)

print(f"Hi {user_name}, I'm the {script} script.") #prints argument 2 and 3 formated neatly in an f string.

print("I'd like to ask you a few questions.") #prints more text

print(f"Do you like me {user_name}?") #Prompts user to answer a question, identifies user based on input from argument 2.

likes = input (prompt)  #sets variable likes equal to whatever the user input is which will be placed in a string

likesfinal="" #defines likesfinal variable as a blank string. We need this variable to be equal to user input in line 26/27 to be then used in line 79. Unlike the variable likes this one we don't mess with and change values.

decisionfinal1 = "" #defines decisionfinal1 variable as a blank string.

if likes == "1":      #The IF statement allows us to record a positive response of 1 as something that makes more sense to read when printed in line 80.

        likesfinal = "Yes"

#The possible_decision variable specifies a list of all userinputs that will be accepted

#by the program. The while function will allow us to get the user stuck in a loop until they answer one of the accepted

#contained in the possible_decision variable. The continue function is what alows us to examine if the while function remains true and subsequently break out of

#the loop when a valid input has been provided.

possible_decision = "No", "NO", "nO", "no", "n", "N", "N0", "n0", "0", "Yes", "yes", "YES", "yES", "yeS", "Y", "y", "1"

negative_decision = "No", "NO", "nO", "no", "n", "N", "N0", "n0", "0"

positive_decision = "Yes", "yes", "YES", "yES", "yeS", "Y", "y", "1"

while likes not in possible_decision:

        print("I did not understand. Did you mean Yes or No?")

        likes = input (prompt)

        likesfinal = likes      

        continue

#The next while function is necessary so that the user can make a conscious decision on wanting

#to kill the program after they indicated they do not like it. Subsequent if statements allow us to validate

#their decision. Setting the likes variable to blank in line 45 is what breaks us out of the loop in case of deciding not to kill the program.

#Line 51 prints out text to prompt the user to make a valid Yes/No selection as a result of invalid input.

while likes in negative_decision:    

        

                try:

                        print(f"would you like to kill {script} with your {weapon} {user_name}?")

                        decisionfinal1 = input(prompt)

                        

                        if  decisionfinal1 in positive_decision:

                                likes = "Yes"

                                likesfinal = "No"

                                continue

                        elif decisionfinal1 in negative_decision:

                                likes = ""

                                likesfinal = "No"

                                continue

                                

                        else:

                                print("I did not understand. Come back when you can type.")

                                continue

                

                except:

                        break

                                

#From line 60-64 we validate that the user wishes to kill the program with an If statement. Some confirmation test is printed and the program quits. 

# Without line 65 the program would just keep running, which isn't the point.

        

if decisionfinal1 in positive_decision:

        

        print(f"{script} is now over. You have killed me.")

        quit()

#Line 70-73 prints a few more questons and requests user input. 

print(f"Where do you live {user_name}?")

lives = input(prompt)

print(f"What kind of computer do you have?")

computer = input(prompt)

#prints some of the question that were asked, text is tabbed twice. We also include a newline to be printed at the end of line 83.

print(f"""

\t\tAlright, so you said {likesfinal} about liking me.

\t\tYou live in {lives}. Not sure where that is.

\t\tAnd you have a {computer} computer. Nice.\n

 """)

#prompts the user to answer a question about the weapon that they are carrying and prints some text depending on the answer followed by quitting the program.

print(f"What's that weapon you are carrying? Is that a {weapon}?")

weaponconfirm = input(prompt)

if weaponconfirm in positive_decision:

        print("Looks cool, don't lose it")

        quit()

elif weaponconfirm in negative_decision:

    print("You liar!, I hope you lose it!")

    quit()

else: print(f"Whatever, {script} gives up!")

quit()

It’s a simple misunderstanding of how the syntax works.

The or operator needs valid expressions on both sides, but in

thing not in this or not in that

the right side, not in that, is not a valid expression, because, again, the in operator also needs valid expressions on both sides. You need to repeat thing:

(thing not in this) or (thing not in that)

(The parentheses are redundant, but they can help you check that each expression is valid.)

In your case:

while likes not in negative_decision or likes not in positive_decision:
    ...
2 Likes

Appreciate the answer! Thanks for your time florian!