'how to keep asking user for right type of input if they type invalid input [duplicate]
This program calculates depreciation
I want to keep asking user for input until it gives the valid input.
def CalculateDep(amount,nofYears,rate,yearOfPurchase):
for i in range(nofYears):
if (mop.lower() == "n" and i == 0):
percentile = rate * 0.005
elif(mop.lower() == "y" or mop.lower() == "n"):
percentile = rate * 0.01
dep = round((percentile)*amount, 2)
print("") # print blank line for readiblity
print("Dep Charge @" + str(percentile*100) + " % : " + str(dep))
print("On 31-03-" + str(yearOfPurchase+1))
amount = amount - (dep)
yearOfPurchase += 1
print("Value After Depreciation : " + str(round(amount, 2)))
amt = int(input("Enter the Amount : ")) #Initial value of asset
y = int(input("Enter the Number of years : "))
r = float(input("Enter The depreciation Rate : "))
yop = int(input("Enter The Year in Which The Asset is purchased : "))
mop = input("Purchased asset before 1st October ? Answer y or n : ")
CalculateDep(amt, y, r, yop)
Solution 1:[1]
Typically this can be accomplished with a while loop
while True:
try:
amt = int(input("Enter the Amount : "))
except ValueError:
print('please enter an int')
else:
break
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tim |