[英]Why doesn't my while loop stop?
I've been reading other posts and couldn't figure it out. 我一直在阅读其他帖子,但无法理解。 No matter what I enter at the end of this repeat it always repeats the loop.
无论我在重复结束时输入什么,它总是重复循环。 I've tried
while(repeat != "Quit" or repeat != "quit" or repeat != "b" or repeat != "B" or repeat != "no" or repeat != "No"):
but it still never worked. 我曾尝试过
while(repeat != "Quit" or repeat != "quit" or repeat != "b" or repeat != "B" or repeat != "no" or repeat != "No"):
但是它仍然没有奏效。 I don't know what else to try. 我不知道还有什么可以尝试的。 What's wrong with this while loop?
这个while循环有什么问题?
repeat = "d"
print "Please answer questions using the choices (A, B, C, etc.)"
time.sleep(2.1738)
while repeat != "Quit" or repeat != "quit" or repeat != "b" or repeat != "B" or repeat != "no" or repeat != "No":
print "A) Round Edges"
print "B) Straight Edges"
Edges1 = raw_input("Does the shape have round edges or straight edges?: ")
if Edges1 == "a" or Edges1 == "A" or Edges1 == "Round Edges" or Edges1 == "round edges":
print "A) Circle"
print "B) Semi-Circle"
Circle = raw_input("Is it a circle or semi-circle?: ")
if Circle == "A" or Circle == "a" or Circle == "Circle" or Circle == "circle":
radius_C = input("What is the radius (1/2 of the Diameter)?: ")
Area_C = math.pi * radius_C ** 2.0
Circum_C = 2.0 * math.pi * radius_C
Diameter_C = 2.0 * radius_C
print "The radius is " + str(radius_C) + ". "
time.sleep(.5)
print "The diameter is " + str(Diameter_C) + ". "
time.sleep(.5)
print "The circumference is " + str(round(Circum_C, 2)) + ". "
time.sleep(.5)
print "The area is " + str(round(Area_C, 2)) + ". "
time.sleep(5)
elif Circle == "B" or Circle == "b" or Circle == "Semi-Circle" or Circle == "semi-circle":
radius_S = input("What is the radius (1/2 of the Diameter)?: ")
Area_S = math.pi * radius_S ** 2.0 * .5
Diameter_S = 2 * radius_S
Per_S = ((math.pi * 2 * radius_S) / 2) + Diameter_S
print "The radius is " + str(radius_S) + ". "
time.sleep(.5)
print "The diameter is " + str(Diameter_S) + ". "
time.sleep(.5)
print "The perimeter is " + str(round(Per_S, 2)) + ". "
time.sleep(.5)
print "The area is " + str(round(Area_S, 2)) + ". "
time.sleep(5)
else:
print "Incorrect input."
elif Edges1 == "b" or Edges1 == "B" or Edges1 == "Straight Edges" or Edges1== "straight edges":
sides = input("How many sides does the shape have?: ")
sideL = input("What is the length of 1 side?: ")
Area = round(Area_R(sides, sideL), 4)
Perim = round(Perm_R(sides, sideL), 4)
print "The area of this figure is: " + str(Area)
print "The perimeter of the figure is: " + str(Perim)
else:
print "Incorrect input."
time.sleep(4)
print" "
print" "
print "A) yes"
print "B) No"
repeat = raw_input("Want to try another?: ")
time.sleep(1)
main()
Well, let's take a look at that condition: 那么,让我们来看看这个条件:
while repeat != "Quit" or repeat != "quit"
Think about it. 想一想。 No matter what the value of
repeat
is, this is always true . 无论
repeat
的价值是什么, 这都是真的 。 repeat
will always be different from "Quit"
OR FROM "quit"
. repeat
将始终不同于"Quit"
或 "quit"
。 If it's the one, it won't be the other. 如果是那个,那就不是另一个。
You need it to be different from "Quit" AND FROM "quit". 你需要它与“退出” 和 “退出”不同。 Try this:
尝试这个:
while repeat != "Quit" and repeat != "quit" ...
Or, the more compact: 或者,更紧凑:
while repeat.lower() not in ["quit", "b"]
Replace your or's with and's like this: 替换你的或者是这样的:
while repeat != "Quit" and repeat != "quit" and repeat != "b" and repeat != "B" and repeat != "no" and repeat != "No":
It's always going to not equal at least one of those things. 它总是不至少与这些东西中的一个相等。 It can't equal two things at once.
它不能同时等于两件事。 Here's another shorter example (but try my suggestion above).
这是另一个较短的例子(但请尝试我的建议)。
# Never true
repeat == "Quit" and repeat == "quit"
# Never false
repeat != "Quit" or repeat != "quit"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.