我想了解異常處理,並想給自己一些練習。下面是我的代碼,它讓用戶輸入一個分數,並將其打印回屏幕上。我的問題是,最終的返回語句沒有返回,所以我得到錯誤NoneType' object is not iterable
。如果用戶不輸入分數,我如何處理這個異常?「NoneType」對象不可迭代「:我知道我的函數什麼也沒有返回,但
def printing_fractions():
frac = str(input("Input a fraction then press enter: "))
try:
n, d = frac.split('/')
int(n)/int(d)
return n,d
except ValueError:
print("You did not enter a fraction")
except ZeroDivisionError:
print("You cannot divide by zero")
return
numerator, denominator = printing_fractions()
print "Your fraction is: " + numerator + "/" + denominator
你想如何「處理」它?也就是說,你想在這種情況下做什麼? – BrenBarn
我想打印「你輸入一個整數,請輸入一個分數」之類的東西 - 或者只是某種錯誤信息。 – ASm
你已經這麼做了。 – BrenBarn