Your issue revolves around variable scope. payment
has a local scope, and therefore the function calc_payment
cannot "see" it. Here I modified your program so that you pass payment
, balance
, and apr
into the calc_payment
function. I also moved m_counter
into the function as well.
def calc_payment(payment, balance, apr)
m_counter = 0
payment_percentage = payment / balance * 100
monthly_apr = apr / 12
while balance > 0
m_counter = m_counter + 1
balance = balance / 100 * monthly_apr
balance = balance - payment
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(payment, balance, apr)