Fix Ruby Error: Undefined Local Variable in Payment Calculator

I tried to make a credit card payment calculator using Ruby, but I’m getting an error. After reviewing the code, I see a few issues that might be causing it. Here’s what I noticed and how I’ll fix them:

Problem in Code:

codem_counter = 0

def calc_payment(balance, apr, payment)
monthly_apr = apr / 12.0
while balance > 0
m_counter = m_counter + 1
balance += (balance * (monthly_apr / 100)) # Interest added
balance -= payment # Payment applied
break if balance <= 0 # Avoid negative balance
end

puts
puts "Monthly payment: $#{payment}"
puts "Balance payoff: #{m_counter} months"
end

puts "Welcome to your credit card payment calculator!"
puts

print "Please tell me your credit card balance: "
balance = gets.chomp.to_f

print "Please enter your interest rate % (APR): "
apr = gets.chomp.to_f

print "How much $ would you like to pay every month? "
payment = gets.chomp.to_f

calc_payment(balance, apr, payment)

Issue In Code:

Method Definition Issue:
I forgot to use parentheses when defining the method. It should be: codedef calc_payment() Functions need parentheses, even if I don’t pass arguments.

Variable Scope Issue:
Inside the calc_payment method, I referenced balance, apr, and payment directly. But they are defined outside the method, and Ruby doesn’t automatically share variables like that between the outside and inside scopes.So, I either need to pass them as arguments to the method or define them globally. I’ll pass them as arguments.

  1. String Concatenation Error:
    In Ruby, I can’t concatenate numbers with strings directly. I need to convert numbers to strings using .to_s.
  2. Logic Error in Interest Calculation:
    I noticed I’m dividing balance by 100 and then applying the APR, which seems incorrect. I think I intended to multiply the monthly APR with the balance to calculate the interest.

Correct Code:

codedef calc_payment(balance, apr, payment)
m_counter = 0 # Resetting counter
monthly_apr = apr / 12 / 100 # Convert APR to a monthly rate in decimal

while balance > 0
m_counter += 1 # Incrementing the month counter
balance += balance * monthly_apr # Adding monthly interest
balance -= payment # Subtracting the monthly payment

# Prevents infinite loop in case of insufficient payment
if m_counter > 500 || balance < 0
break
end
end

puts
puts "Monthly payment: $#{payment}"
puts "Balance payoff: #{m_counter} months"
end

puts "Welcome to your credit card payment calculator!"
puts

puts "Please tell me your credit card balance."
balance = gets.chomp.to_f

puts "Please enter your interest rate %."
apr = gets.chomp.to_f

puts "How much $ would you like to pay every month?"
payment = gets.chomp.to_f

calc_payment(balance, apr, payment)

Explanation of Changes:

  1. Modularization with Parameters:
    • The calc_payment function now accepts balance, apr, and payment as parameters, making the function reusable and easy to test.
  2. Monthly APR Conversion:
    • The line monthly_apr = apr / 12 / 100.0 converts the annual percentage rate (APR) into a monthly interest rate in decimal form.
    • For example, if APR = 12%, then the monthly APR will be 12/12/100=0.0112 / 12 / 100 = 0.0112/12/100=0.01 or 1%.
  3. Monthly Interest Calculation:
    • Each month, the balance accrues interest: interest = balance * monthly_apr.
  4. Loop Control and Balance Handling:
    • The while loop runs until the balance becomes zero or less.
    • To ensure the program doesn’t run infinitely when the balance becomes slightly negative, a break statement stops the loop if the balance drops below or equal to zero.
  5. Output Formatting:
    • The puts statements are improved using string interpolation (#{}) to make the code cleaner and more readable.
  6. User Input Handling:
    • The code uses gets.chomp.to_f to safely convert user inputs to floating-point numbers for calculations.

How the Code Works:

  1. Welcome Message:
    • The program prints a welcome message to the user.
  2. User Input:
    • The program prompts the user to enter the credit card balance, APR (interest rate), and the monthly payment they wish to make.
  3. Payment Calculation:
    • The calc_payment function calculates how many months it will take to pay off the balance with the given monthly payment and APR.
    • It loops through each month, applying the monthly interest to the remaining balance, subtracting the user’s payment, and incrementing the month counter.
  4. Result Display:
    • Finally, the program prints the total monthly payment and how many months it will take to pay off the credit card balance.

This revised version ensures the program is more readable, modular, and efficient, with cleaner logic and better handling of inputs and outputs.

Related blog posts