在Python中检查2个不同的用户输入是否存在循环

时间:2019-03-07 03:20:20

标签: python python-3.x

我对此并不陌生,尝试让循环遍历一系列数字,直到2个不同的用户对特定数字说“是”。

如果两个用户都对数字说“是”,则我希望程序打印“你们都选择了[X]!”。 X是数字,循环在这一点停止。

但是,如果任何一个用户拒绝,我希望循环继续到下一个数字。为什么以下代码无法实现此目的?

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

5 个答案:

答案 0 :(得分:1)

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

读为

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

因此user1_response == "N"可能返回TrueFalseNoReject实际上都将返回True

您可能想要的是

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

因此,我们创建了一个文字集{"N","No","Reject"},我们正在查看user1_response是否在该集中。

答案 1 :(得分:0)

代码"No" or "Reject"被转换为布尔值True。您将需要检查输入是否分别相等。

答案 2 :(得分:0)

在这里,尝试这个:

#starts our counter
counter = -1
#list of strings that are classified as 'rejections'
rejections = ["no", "N", "n", "Reject"]
#starts our while loops
while counter < 100:
    #adds one to the counter each time the loop executes
    counter = counter + 1

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

    else:

        print("")

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

        break

(是的,我知道可能比在-1处启动计数器更好,但可以完成工作)

edit:仅将其修复为使用for而不是while循环

#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

答案 3 :(得分:0)

我对此也很陌生,但希望我可以通过增加与您的技能水平相近的观点来提供帮助

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)

我认为,如果您要与他人分享自己的创作成果,那么该解决方案将非常易于理解,并且对于其他新开发人员也非常容易使用。

答案 4 :(得分:0)

条件是有问题的。

(user1_response ==“ N”或“否”或“拒绝”)仅在user1_response ==“ N”时计算为True

但是,如果user1和user2都键入“ Y” (user1_response ==“ N”或“ No”或“ Reject”)计算为“ No” 和 (user2_response ==“否”或“ N”或“拒绝”)计算为“ N”

组合语句(user1_response ==“ N”或“否”或“拒绝”)或(user2_response ==“否”或“ N”或“拒绝”)的计算结果为“否”

和“否”将触发代码的“ if”部分,因为“否”不等于布尔False。因此,将不会到达代码的“其他”部分。

这是一种可能的解决方案:

if user1_response in ("N","No","Reject") or user2_response in ("No","N","Reject"):