英文:
"or" & "and" in loop (while) structure
问题
我尝试创建一个简单的程序,询问用户的性别。它只接受两种输入:"M"或"F"。
然后:
sex = str(input('[ M / F ]: ')).upper().strip()
while sex != 'M' and sex != 'F':
print('error')
sex = str(input('try again [ M / F ]: ')).upper().strip()
print('It\'s done :)')
但只有在循环中使用"and"时才有效。这不是一个大问题,但我真的想知道为什么...
英文:
I was trying to make a simple program that asks for the sex of the user. And the only two inputs it accepts it is: "M" or "F".
Then:
sex = str(input('[ M / F ]: ')).upper().strip()
while sex != 'M' or sex != 'F':
print('error')
sex = str(input('try again [ M / F ]: ')).upper().strip()
print('It's done :)')
But it only works if i use the term "and" in the loop. It is not a big question but i really wanted to know why...
答案1
得分: 4
For your while
loop to break you need both conditions to be False
, this would mean sex
being both M
and F
at the same time.
This is never possible.
Corrected code:
sex = str(input('[ M / F ]: ')).upper().strip()
while sex != 'M' and sex != 'F':
print('error')
sex = str(input('try again [ M / F ]: ')).upper().strip()
print('It\'s done :)')
Or using De Morgan's equivalence:
sex = str(input('[ M / F ]: ')).upper().strip()
while not (sex == 'M' or sex == 'F'):
print('error')
sex = str(input('try again [ M / F ]: ')).upper().strip()
print('It\'s done :)')
Or the classical pythonic way:
sex = str(input('[ M / F ]: ')).upper().strip()
while sex not in {'F', 'M'}:
print('error')
sex = str(input('try again [ M / F ]: ')).upper().strip()
print('It\'s done :)')
英文:
For your while
loop to break you need both conditions to be False
, this would mean sex
being both M
and F
at the same time.
This is never possible.
Corrected code:
sex = str(input('[ M / F ]: ')).upper().strip()
while sex != 'M' and sex != 'F':
print('error')
sex = str(input('try again [ M / F ]: ')).upper().strip()
print('It\'s done :)')
Or using De Morgan's equivalence:
sex = str(input('[ M / F ]: ')).upper().strip()
while not (sex == 'M' or sex == 'F'):
print('error')
sex = str(input('try again [ M / F ]: ')).upper().strip()
print('It\'s done :)')
Or the classical pythonic way:
sex = str(input('[ M / F ]: ')).upper().strip()
while sex not in {'F', 'M'}:
print('error')
sex = str(input('try again [ M / F ]: ')).upper().strip()
print('It\'s done :)')
答案2
得分: 1
让我们将3种情况放入一个真值表中:
----------------------------------------------------------
性别 | 性别 != 'M' | 性别 != 'F' | |
| A | B | A 或 B | A 和 B
----------------------------------------------------------
X | 真 | 真 | 真 | 真
M | 假 | 真 | 真 | 假
F | 真 | 假 | 真 | 假
----------------------------------------------------------
从表格中可以看出,和显然是正确的选择,而或总是返回真。
英文:
Let's put 3 cases into a truth table:
----------------------------------------------------------
sex | sex != 'M' | sex != 'F' | |
| A | B | A or B | A and B
----------------------------------------------------------
X | True | True | True | True
M | False | True | True | False
F | True | False | True | False
----------------------------------------------------------
As you can see from the table, and is clearly the correct choice, while or always returns True.
答案3
得分: 1
你可以按照以下方式减少代码量并同时使其更加健壮:
while (sex := input(' [ M / F ]: ')).upper().strip() not in 'MF':
print('error\ntry again ', end='')
# 这里可以使用 sex 变量
print('It\'s done :)')
通过使用 in 而不是 and 或错误的 or 条件,您将简化该过程。还请注意,input() 返回的是 str,所以调用 str() 是不必要的。它会起作用,但是没有必要。
英文:
You can reduce the amount of code and at the same time make it more robust as follows:
while (sex := input('[ M / F ]: ')).upper().strip() not in 'MF':
print('error\ntry again ', end='')
# sex variable could be used here
print('It\'s done :)')
By using in rather than and or the erroneous or condition you will have simplified the process. Note also that input() returns str so the call to str() is irrelevant. It will work but it's unnecessary
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论