2015-10-14 28 views
0

我已經做了約4周的編程現在。愛它到目前爲止。但是我一直在寫這段代碼。這是關於形狀的區域。我試圖看看這裏,看到一些線程,但它沒有幫助。 當我不想要它時,它會保持循環。保持循環。嘗試其他線程,沒有工作

更新時間:

#Area of shapes 
shape=[] 
choice= None 
while choice !="0": 
    print (
     """ 
Choices: 
0. Exit 
1.Square 
2.Rectangle 
3.Triangle 
4.Trapezium 
    """ 
     ) 

    choice = input ("Choice\n") 
    print() 

#Exit 
if choice != "0": 
    print ("Incorrect, please try again") 

#Square 
    elif choice in ["1", "Square"]: 
     square1=int(input("Please input the side of the square\n")) 
     print ("The area of the square is", square1*4,) 


#Rectangle 
    elif choice in ["1", "Rectangle"]: 
     rectangle1 = int(input ("Please input the side of the reactanglezn")) 
     rectangle2 = int(input ("Please input the other side of the reactangle\n")) 
     arearect== rectangle1*rectangle2 
     print ("The area of the rectangle is of", arearect) 

#Triangle 
    elif choice in ["1", "Triangle"]: 
     triangle1 = int(input ("Please input the base of the triangle")) 
     triangle2 = int(input ("Please input the height of the triangle")) 
     areatri =0.5*triangle1*triangle2 
     print ("The area of the triangle is", areatri) 

#Trapezium 
    elif choice in ["1", "Trapezium"]: 
     trapezium1 = int(input ("Please input the side A of the trapezium")) 
     trapezium2= int(input ("Please input the side B of the trapezium")) 
     trapezium3= int(input ("Please input the height of the trapezium")) 
     areatrap= trapezium1*trapezium2/2*trapezium3 
     print ("The area of the trapezium is", areatrap) 

    else: 
     print ("Invalid Choice") 
+3

縮進是錯誤的。您必須在'while'下面至少縮進一行:'choice = input()'。 – Boldewyn

+0

您能澄清問題中顯示的縮進是否與您的實際代碼相匹配。另外,你能準確地展示發生了什麼?輸入和麪積計算是永久重複的,還是隻是初始提示? – Blckknght

+0

永遠重複@Blckknght –

回答

0

試着改變你的if語句看起來像這樣

If choice == "1" or choice == "Square" 

有了你現在的樣子,你不檢查,看看是否選擇等於字符串「正方形「,你正在檢查是否存在字符串」Square「。

編輯:我剛剛意識到這實際上並沒有回答你的循環問題,但是一旦循環解決,你就會遇到這個問題。 要解決它,你需要將你的輸入轉換爲一個字符串,因爲你將它與一個字符串值進行比較。爲此,您可以執行以下操作:

choice str(input("Choice\n")) 

代替您現在擁有的功能。

+0

把一個'str'到處打電話'input'不會做任何事情在Python 3中很有用,我猜測提問者使用'print()'函數調用 – Blckknght

+0

啊,不是嗎?我使用2.6.1測試了它,這是trypython.org使用的(我的工作計算機沒有安裝它。)print()函數調用也存在於該版本中,所以我不確定。 – Tofystedeth

+0

'print'是Python 2中的一個語句,而不是一個函數(除非通過將'from __future__ import print_function'放在代碼的其餘部分之上來請求更新的行爲。您仍然可以將參數加括號,但如果有多個參數,您將打印一個元組而不是空格分隔的值(將一個參數括起來不會做任何事情)。 – Blckknght

0

你需要在第14行(改變選擇的值的那個)的縮進來糾正你的語法。

注重縮進,因爲它們很容易破壞你的代碼的功能,如果/別人的,等

0

您需要縮進大部分代碼,以便它是你的頂級循環中。目前,您在獲得choice後立即致電print(),這就意味着除了0之外的任何內容都不會執行任何操作。輸入0將退出循環,但不會執行任何區域計算。

你的代碼的結構應該是這樣的:

while choice != "0": 
    # print choices 
    choice = input ("Choice\n") 
    print() 

    if choice == "0": 
     # print exit message 
    elif choice in ["1", "Square"]: 
     # compute area of square 
    elif choice in ["1", "Rectangle"]: 
     # compute area of rectangle 
    # other options 
    else: 
     # invalid choice 

我也與你elif條件,你在哪裏不當檢查複式值固定的問題。這是if choice == "1" or choice == "Square"的替代方案,它也可以正常工作。

+0

我將代碼更新爲標準@Blckknght,它表示elif是無效的語法。 –

+0

您的第一個'if'語句和它下面的'print'不會像應該的那樣縮進。縮進在Python中很重要,你必須明白! – Blckknght