我为Can not get “while” statement to progress写了这个答案,但在我提交之前它被标记为重复。它是一个精确的逻辑重复(x != foo or y != bar
),所以我在这里发布这个,希望我的回答可能对某人有所帮助。
答案是逐字复制的。
你的问题在这里:
while username != logindata.user1 or username != logindata.user2:
...
while password != logindata.passw1 or password != logindata.passw2:
The username loop in English is something like, "Keep looping if the provided username
is either not equal to user1
, or not equal to user2
." Unless user1
and user2
are the same, no string will ever let that evaluate to False
. If 'username' is equal to user1
, it cannot be equal to user2
(again, assuming user1 != user2
, which is the case here).
The quick fix is to change the or
to and
. That way, you're checking whether username
is not either of the available options. A better way to write that would be:
while not (username == logindata.user1 or username == logindata.user2):
But I would say that the correct way to write it, given what you're trying to do, is this:
while username not in [logindata.user1, logindata.user2]:
In English, something like, "Keep looping while the username is not in this list of usernames."
P.S. I was going to use a set instead of a list, for pedantic correctness, but it really doesn't matter for this, so I figured a list would be easier for a beginner. Just mentioning it before someone else does :).