1

I'm new to this and trying to have a loop iterate through a series of numbers until 2 different users say yes to a specific number.

If both users say yes to a number, I'd like the program to print "You both picked [X]!" with X being the number, and for the loop to stop at this point.

If either user says no, however, I'd like the loop to simply continue on to the next number. Why does the below code not work to achieve this?

for i in range(100):
    user1_response = input("User One: Would you like to pick " + str(i) + "? ")
    user2_response = input("User Two: Would you like to pick " + str(i) + "? ")

    if (user1_response == "N" or "No" or "Reject") or (user2_response == "No" or "N" or "Reject"):
        continue

    else:
        print("")
        print("Both users picked" + str(i) + "!")
        break
Dave S
  • 788
  • 1
  • 11
  • 30
deevee
  • 11
  • 5
  • 2
    Possible duplicate of [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – iz_ Mar 07 '19 at 03:22

5 Answers5

0
 if (user1_response == "N" or "No" or "Reject") 

is being read as

 if ([[user1_response == "N"]] or [["No"]] or [["Reject"]]) 

so user1_response == "N" might return True or False. The No and Reject will both essentially return True.

What you might want is

 if (user1_response in {"N","No","Reject"}) 

So we created a literal set {"N","No","Reject"}, and we are seeing if user1_response is in that set.

Sidd Singal
  • 559
  • 6
  • 16
0

The code "No" or "Reject" gets converted to a boolean True. You will need to check if the input equals each individually.

Azmisov
  • 6,493
  • 7
  • 53
  • 70
0

Here, Try this:

#list of strings that are classified as 'rejections'
rejections = ["no", "N", "n", "Reject"]
#starts our while loops
for i in range(100):

    user1_response = input("User One: Would you like to pick " + str(i) + "? ")
    user2_response = input("User Two: Would you like to pick " + str(i) + "? ")
    if user1_response in rejections or user2_response in rejections:
        continue

    else:

        print("")

        print("Both users picked " + str(i) + "!")

        break
Ben Caunt
  • 9
  • 5
  • Have a look at [itertools#count](https://docs.python.org/3/library/itertools.html#itertools.count), instead of a while loop. – Dillon Davis Mar 07 '19 at 03:41
0

I’m pretty new to this as well, but hopefully I can help by adding a perspective close to your skill level

neg= ["n", "no", "reject"]
pos= ["y", "yes", "accept"]
##positive and negative lists to easily check for a     cass unsensitive response

for i in range(100):
    user1= input("User 1 : Would you like to pick "+      str(i)+ "?")
    user2= input("User 2 : Would you like to pick "+ str(i)+ "?")
 ## I suggest making your variable names a little shorter


##Adding .lower() allows users to enter "n' or "N" etc.
    if user1.lower() in neg or user2.lower() in neg:
    continue



    elif user1.lower() in pos and user2.lower() in pos:
            break
print"Both users guessed number "+str(i)

In my opinion this solution is fairly simple to understand, and very accessible to other new develops, if you were to share what you’ve made with others.

0

The conditional is problematic.

(user1_response == "N" or "No" or "Reject") evaluates to True only when user1_response == "N"

However, if both user1 and user2 type "Y" (user1_response == "N" or "No" or "Reject") evaluates to "No" and (user2_response == "No" or "N" or "Reject") evaluates to "N"

the combined statement (user1_response == "N" or "No" or "Reject") or (user2_response == "No" or "N" or "Reject") evaluates to "No"

and "No" will trigger the "if" portion of the code because "No" is not equivalent to the boolean False. Therefore the "else" portion of the code will not be reached.

This is one possible solution:

if user1_response in ("N","No","Reject") or user2_response in ("No","N","Reject"):
wontleave
  • 177
  • 1
  • 10