New help on correcting this problem


#1

Thats the code that I did and didnt get the full credit. He reply and said “Add Validations in the code instead of asking/expecting a valid input from user. Like values should be positive integer and all”. What does he mean and how do I do that?

Question: (Financial application: loan amortization schedule) The monthly payment for a given loan pays the principal and the interest. The monthly interest is computed by multiplying the monthly interest rate and the balance (the remaining principal). The principal paid for the month is therefore the monthly payment minus thenmonthly interest. Write a program that lets the user enter the loan amount, number of years, and interest rate, and then displays the amortization schedule for the loan.

loanAmount = eval(input("Enter loan amount, for example 120000.95: "))


numOfYears = eval(input("Enter number of years as an integer, for example 5: "))


annualInterestRate = eval(input("Enter yearly interest rate, for example 8.25: "))


monthlyInterestRate = annualInterestRate/1200


monthlyPayment = loanAmount*monthlyInterestRate / \
    (1 - (pow(1 / (1 + monthlyInterestRate), numOfYears * 12)))

balance = loanAmount
print("Monthly Payment:", int(monthlyPayment * 100) / 100.0)
print("Total Payment:", int(monthlyPayment * 12 * numOfYears * 100) / 100.0)


print(format("Payment#", "<15s"), format("Interest", "<15s"), format("Principal", "<15s"), format("Balance", "<15s"))
for i in range(1, numOfYears * 12 + 1):
    interest = int(monthlyInterestRate * balance * 100) / 100.0
    principal = int((monthlyPayment - interest) * 100) / 100.0
    balance = int((balance - principal) * 100) / 100.0
    print(format(i, "<15d"), format(interest, "<15.2f"), format(principal, "<15.2f"), format(balance, "<15.2f"))

#2

why would you use eval()? this will cause the user input to be evaluated as code, which makes this program very vulnerable

look:

Screenshot_2018-02-14_10-38-39

if the user enters a string, your program crashes. The user could enter a negative number (-10000 for example. You need to ensure this can’t happen, i can help you, but first, i have to ask: How do you think we can handle this?


#3

changing eval to int and float


#4

I used eval cause the author of the book I’m using uses it alot.


#5

what if the user enters a string? Then casting to float throws an error

so if he jumps a ditch or off a building, you would do the same? I hope you understand the point i am trying to make, understand what you are doing, the things you are using


#6

Yeah I get you. I was thinking maybe a if statement maybe.


#7

so then we would need to do:

loanAmount = input("Enter loan amount, for example 120000.95: ")
if something:
    # convert to float

but what condition would we need? its a good idea, but its tricky

thankfully, python offers try/except which allows use to catch errors:

try:
    float("i am going to fail")
except ValueError:
   print("Error!!")

i wanted to see you put thought in how you could handle such a thing :slight_smile:


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.