2015-11-02 138 views
0

下面的程序獲取用戶輸入並從輸入的數字中輸出最小值和最大值,但它總是打印「無」,我找不到任何錯誤(顯然我錯過了一些東西) 。任何人都可以幫助我理解錯誤。用戶輸入的最小值和最大值

largest = None 
smallest = None 

while True: 
    num1 = raw_input("Enter a number: ") 
    if num1 == "done" : break 

    try: 
     num=int(num1) 
    except: 
     print 'Invalid input' 
     continue 

     if smallest is None or num < smallest: 
      smallest = num 

     if largest is None or num > largest: 
      largest = num 

print "Maximum is", largest 
print "Minimum is", smallest 
+0

正確的縮進在Python中至關重要。檢查你的。 – MattDMo

回答

2

您的if語句過於縮進。

largest = None 
smallest = None 

while True: 
    num1 = raw_input("Enter a number: ") 
    if num1 == "done" : break 

    try: 
     num=int(num1) 
    except: 
     print 'Invalid input' 
     continue 

    if smallest is None or num < smallest: 
     smallest = num 

    if largest is None or num > largest: 
     largest = num 

print "Maximum is", largest 
print "Minimum is", smallest 
+0

謝謝傑森。我感謝您的幫助。我將非常小心今後的縮進。 – CrystalZ

相關問題