2014-02-18 29 views
0

我是編碼方面的新手,對於我在做什麼我很少有想法。所以這可能是非常錯誤的。但是我必須創建一個確定斜率和Y截距的代碼。我創建了一個循環來確定是否有任何座標是相同的,如果它們是必須更改的,但我無法擺脫循環。這是我目前的循環。任何幫助將不勝感激!提前致謝!我需要在while語句中使用多個條件

def whileloop(): 

while True: 
    y1 == y2 or x1 == x2 


    print ("Error") 




      print("please enter values that are not equal to eachother.") 
      y1_str = input ("please enter y1: ") 
      y2_str = input ("please enter y2: ") 



      print ("please enter values that are not equal to eachother.") 
      x1_str = input ("please enter x1: ") 
      x2_str = input ("please enter x2: ") 


else: y1!= y2 or x1!=x2 
+0

請修復首先是格式化。 – abarnert

+0

無論如何,第一個問題是你有一個'while True',並且你沒有'break'(或'return')任何地方,所以當然這個循環將會一直持續下去。如果你想在某個時候突破它,你必須做一些事情,如'如果<破壞理由>:break'。 – abarnert

+0

接下來,'y1 == y2或x1 == x2'只是一個表達式。無論是真是假都沒關係,因爲你忽略了它的價值。你可能在這裏想要一個'if'語句:'if y1 == y2 or x1 == x2:'。然後,只有當表達式爲真時,'if'語句中的代碼纔會運行(縮進到它的右側)。 – abarnert

回答

0

認爲你想要的是這樣的:

def whileloop(): 
    while True: 
     y1_str = input("please enter y1: ") 
     y2_str = input("please enter y2: ") 
     x1_str = input("please enter x1: ") 
     x2_str = input("please enter x2: ") 
     y1 = float(y1_str) 
     y2 = float(y2_str) 
     x1 = float(x1_str) 
     x2 = float(x2_str) 

     if y1 == y2 or x1 == x2: 
      print("please enter values that are not equal to eachother.") 
     else: 
      return y1, y2, x1, x2 
0

在一段時間/使用多個條件,如果/ elif的語句是非常簡單的一種方式。像這樣的東西能正常工作

while A and B: 

while A or B: 

while A and not B: 

while (A and B) or C: 

可能性是無窮的 - 只要確保明確界定每個條件,如果在括號清晰度疑問獨立的條件。爲了您的代碼將專門

while y1 == y2 or x1 == x2: 
    print("please enter values that are not equal to eachother.") 

    y1_str = input("please enter y1: ") 
    y2_str = input("please enter y2: ") 
    x1_str = input("please enter x1: ") 
    x2_str = input("please enter x2: ") 

    y1 = float(y1_str) 
    y2 = float(y2_str) 
    x1 = float(x1_str) 
    x2 = float(x2_str) 


# Do stuff with y1, y2, x1, x2 

此方法需要您已經有y1y2x1x2定義 - 如果你想在while循環來定義它們,然後abarnert的方法會工作得很好

相關問題