所以我有一個簡單的這個函式,它在出錯時被自己再次呼叫,直到它沒有錯誤地回傳。
def enter_r_h() -> (float, float):
try:
r = float(input("\n--> Enter the radius of the cylinder\n"))
h = float(input("--> Enter the height of the cylinder\n"))
except ValueError:
print("--> Values aren't entered correctly\n")
return enter_r_h()
return r, h
當用戶輸入錯誤值時,這是一個寫得很差的解決方法,因為理論上這可能會導致堆疊溢位(python 中有這樣的事情嗎?)或 RecursionError。
uj5u.com熱心網友回復:
為什么不把它放在一個 while 回圈中,這樣你就不用構建堆疊了?然后你也可以計算嘗試的次數,如果用戶不知情就殺死程式。
另一方面,您不太可能用手動輸入的嘗試來填充堆疊。用戶可能會在它裝滿之前因年老而死亡。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/362922.html
上一篇:如果不需要范圍查詢,B -tree與Hash相比,對于記憶體資料庫有什么優勢嗎?
下一篇:比較PHP中的日期
