2012-09-16 175 views
0

你好,我想讓我的輸入只允許整數,一旦它超過10,它顯示錯誤,任何援助將不勝感激。python輸入錯誤檢查

square_ct = input("Enter an integer from 1-5 the number of squares to draw: ") 
triangle_ct = input("Enter an integer from 1-5 the number of triangles to draw: ") 

while square_count(input) > 10: 
    print ("Error!") 
    square_count=input() #the statement reappears 

while triangle_count(input) > 10: 
    print ("Error!") 
    triangle_count=input() #the statement reappears 
+0

解決您的壓痕。 –

回答

3

我的首選方法是使用while True環路與一休:

while True: 
    square_ct = input("Enter an integer from 1-5 the number of squares to draw: ") 
    if square_ct <= 10: break 
    print "Error" 

# use square_ct as normal 

或者Python的3:

while True: 
    square_ct = int(input("Enter an integer from 1-5 the number of squares to draw: ")) 
    if square_ct <= 10: break 
    print("Error") 

# use square_ct as normal 
+0

那麼我應該爲每個問題做兩個單獨的真實陳述嗎?我得到錯誤TypeError:無法訂購的類型:str()> int() –

+0

您使用Python 3嗎? – nneonneo

+0

是的,從使用print()的樣子... – Marko