簡體   English   中英

無法退出python中的循環

[英]Not able to exit the loop in python

我是 Python 的新手,正在嘗試執行以下代碼。 該程序用於計算復利。

我創建了基本程序並將值作為參數傳遞並且它起作用了。 后來我創建了用戶輸入程序並且它起作用了。

現在,當我嘗試處理負值或 0 值時,我無法退出程序

def CompI(amt, r, t):
    return amt * (pow((1 + r / 100), t)) 

print("Enter Amount")
amt = int(input())
if amt<0 or amt == 0:
    print("Invalid Input")
    exit()

print("Enter rate")
r = int(input())
if r<0 or r == 0:
    print("Invalid Input")
    exit()

print("Enter Time")
t = int(input())
if t<0 or t == 0:
    print("Invalid Input")
    exit()

CI = CompI(amt, r, t)
print("Result is", CI)

結果


輸入金額 0 無效輸入 輸入匯率


當我輸入金額為 0 時,它會顯示錯誤消息並進一步要求我輸入匯率。

也許嘗試import sys然后使用sys.exit()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM