【发布时间】:2021-10-27 09:46:25
【问题描述】:
目前我的代码在检测到错误时退出,例如,如果您的收入输入为负数,它将退出,如果您的邮政编码作为字符串输入,它将退出。这很好,但我需要能够创建一个循环,这样它会给你另一个机会在不退出的情况下重新输入你的值。对python非常陌生,还没有弄清楚。任何帮助将不胜感激:)
# Initialize the constants
TAX_RATE = 0.15
TAX_RATE_MARRIED = 0.14
STANDARD_DEDUCTION = 10000.0
DEPENDENT_DEDUCTION = 3000.0
# Request the inputs
taxFile = input("Enter your tax file number: ")
grossIncome = float(input("Enter the gross income: "))
if (grossIncome < 0):
print("Your income cannot be negative", )
exit()
numDependents = int(input("Enter the number of dependents: "))
firstName = input("Enter your first name: ")
lastName = input("Enter your last name: ")
marrigeStatus = input("Are you Married? True/False: ")
address = input("Enter your address: ")
try:
postcode = int(input("Enter your postcode: "))
except:
print("Invalid Postcode Input")
exit()
# Compute the income tax
taxableIncome = grossIncome - STANDARD_DEDUCTION - numDependents * DEPENDENT_DEDUCTION
incomeTax = taxableIncome * TAX_RATE
incomeTaxMarried = taxableIncome * TAX_RATE_MARRIED
# Display the income tax
print("The tax file number is", taxFile)
print("Your employee information is", firstName, lastName)
print("Your address is", address, postcode)
print("Your total income for the annual year was $", grossIncome)
if marrigeStatus:
print("Your total income tax is $" + str(round(incomeTaxMarried, 2)))
if not marrigeStatus:
print("Your total income tax is $" + str(round(incomeTax, 2)))
print("Thank you for using the program please press enter to close the program")
【问题讨论】:
-
欢迎来到 Stack Overflow!请使用tour 并阅读How to Ask。以后,请写一个更具描述性的标题。 “Python 问题”是多余的,因为这是一个问答网站,并且您正确使用了标签 python。我会自己删除它,但标题的其余部分太模糊了。也许更好的是“当输入无效时如何避免退出?”也就是说,已经有a canonical question on this topic,所以不用担心editing。
标签: python python-3.x loops