使用带有 2 个参数的 While not function
Using While not function with 2 arguments
我需要检查输入是否正确,如果没有重复输入过程,但我使用的代码不能很好地将正确的输入识别为正确的输入。请给我一个解决方案。我是 python.
的初学者
cat_behaviour= []
print("\nIs the cat (A)ggressive or (F)riendly?\n")
behaviour = input().upper()
while not behaviour == 'A' or not behaviour == 'F':
print("You have entered a wrong input. Please enter A for aggressive or F for Friendly")
behaviour = input().upper()
cat_behaviour.append(behaviour)
behaviour = input().strip().upper()
while behaviour not in "AF":
...
.strip()
将确保您不会得到任何额外的空格或行尾字符。更改的 while 条件只是为了更容易理解。您的问题是,您使用的是 or
而不是 and
(因此只有当字符同时为 A
和 F
时才会退出)。
我需要检查输入是否正确,如果没有重复输入过程,但我使用的代码不能很好地将正确的输入识别为正确的输入。请给我一个解决方案。我是 python.
的初学者cat_behaviour= []
print("\nIs the cat (A)ggressive or (F)riendly?\n")
behaviour = input().upper()
while not behaviour == 'A' or not behaviour == 'F':
print("You have entered a wrong input. Please enter A for aggressive or F for Friendly")
behaviour = input().upper()
cat_behaviour.append(behaviour)
behaviour = input().strip().upper()
while behaviour not in "AF":
...
.strip()
将确保您不会得到任何额外的空格或行尾字符。更改的 while 条件只是为了更容易理解。您的问题是,您使用的是 or
而不是 and
(因此只有当字符同时为 A
和 F
时才会退出)。