Undefined local variable | Ruby Beginner

I just started learning Ruby a few weeks ago and tried to make a credit
card payment calculator today to practice but I’m getting an error
message: ‘calc_payment’: undefined local variable or method ‘payment’
for main:Object (NameError)

Could someone help me out? As I see it, I defined all the variables so I
don’t understand why I’m getting the error message.

Here’s the whole code:

m_counter = 0

def calc_payment
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

You will need to pass those in as parameters

def calc_payment(payment, balance, apr)

end

and then call it with

Oops mistyped there. You should have something like this

def calc_payment(payment, balance, apr)
payment_percentage = payment / balance * 100
monthly_apr = apr / 12

m_counter = 0
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)

The points to note is that m_counter is now defined inside the function
and
that the printing is done in a more Rubyish way.

However your code still has a problem…

balance = balance / 100 * monthly_apr
balance = balance - payment

This will result is in the balance being paid off after a month.

Welcome to your credit card payment calculator!

Please tell me your credit card balance.
1500
Please enter your interest rate %.
12
How much $ would you like to pay every month?
200

Monthly payment: $200.0
Balance payoff: 1 months

Thank you for your help!
I modified it and also noticed the problem with the formula.
So this is what I got. It looks like it works, I think there are only
problems with the calculator part. Somehow the results aren’t the same
as some online calculators. But as long as the code works, I’m happy. :slight_smile:

def calc_payment(payment, balance, apr)
m_counter = 0
total = balance
payment_percentage = payment / balance * 100
monthly_apr = apr / 12

while balance > 0
m_counter = m_counter.to_i + 1
month_interest = balance / 100 * monthly_apr
balance = balance + month_interest
balance = balance - payment
total = total + month_interest
end

puts
puts “Monthly payment: $#{payment}”
puts “Balance payoff: #{m_counter} months”
puts “Total payments: $” + total.round(2).to_s

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)

Am 12.10.2013 17:11, schrieb Greg H.:

Thank you for your help!
I modified it and also noticed the problem with the formula.
So this is what I got. It looks like it works, I think there are only
problems with the calculator part. Somehow the results aren’t the same
as some online calculators. But as long as the code works, I’m happy. :slight_smile:

Differences might be caused by integer division (just a guess without
going through the calculation in detail):

2.0.0-p247 :001 > 8 / 5
=> 1
2.0.0-p247 :002 > 8.0 / 5
=> 1.6
2.0.0-p247 :003 > 8.to_f / 5
=> 1.6

Regards,
Marcus

def calc_payment(payment, balance, apr)
m_counter = 0
total = balance
payment_percentage = payment / balance * 100
monthly_apr = apr / 12

[…]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs