2015-10-08 14 views
5

Tôi đã đọc các bài đăng khác và không thể tìm ra. Không có vấn đề gì tôi nhập vào cuối lặp lại này nó luôn lặp lại vòng lặp. Tôi đã thử while(repeat != "Quit" or repeat != "quit" or repeat != "b" or repeat != "B" or repeat != "no" or repeat != "No"): nhưng nó vẫn chưa bao giờ hoạt động. Tôi không biết những gì khác để thử. Có gì sai với vòng lặp trong khi này?Tại sao vòng lặp while của tôi dừng lại?

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() 
+3

Tôi nghĩ bạn có nghĩa là 'and' thay vì 'or' ... – ThiefMaster

Trả lời

7

Vâng, chúng ta hãy nhìn vào điều kiện:

while repeat != "Quit" or repeat != "quit" 

Hãy suy nghĩ về nó. Bất kể giá trị của repeat là, , điều này luôn đúng. repeat sẽ luôn khác với "Quit"HOẶC TỪ"quit". Nếu đó là một, nó sẽ không phải là khác.

Bạn cần nó khác với "Thoát" VÀ TỪ "thoát". Hãy thử điều này:

while repeat != "Quit" and repeat != "quit" ... 

Hoặc, nhỏ gọn hơn:

while repeat.lower() not in ["quit", "b"] 
+0

Wow một sửa chữa đơn giản như vậy:/ –

+2

Có lẽ, tôi phát hiện nó dễ dàng hơn bởi vì tôi đã không đọc tất cả các mã trong vòng lặp - tôi cho rằng vấn đề sẽ nằm trong các dòng đầu tiên hoặc cuối cùng. Lần tới, khi bạn không thể tìm thấy vấn đề, ** tìm đoạn mã nhỏ nhất có thể gặp vấn đề ** và tấn công :) – slezica

+0

Xin cảm ơn sự trợ giúp :) –

0

Thay thế hoặc của bạn với và của như thế này:

while repeat != "Quit" and repeat != "quit" and repeat != "b" and repeat != "B" and repeat != "no" and repeat != "No": 

Nó luôn luôn không bằng ít nhất một trong những điều đó. Nó không thể bằng hai thứ cùng một lúc. Đây là một ví dụ ngắn hơn (nhưng hãy thử đề nghị của tôi ở trên).

# Never true 
repeat == "Quit" and repeat == "quit" 

# Never false 
repeat != "Quit" or repeat != "quit" 
Các vấn đề liên quan