Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Alphonse Cuccurullo
Alphonse Cuccurullo
2,513 Points

Having a issue combining my when statements with my if statement's can someone help me?

debt = {american_express: 500, visa: 900, Mickey_mouse_club: 300}

puts "To pay the debt press pay " puts "To postpone payment press pospone" puts "To Run away and not pay please tell us to go fuck ourselve's "

decision = gets.chomp

case decision

when "pay" puts "Please choose the credit card you owe a balance too.\n"

credit_card = gets.chomp.downcase

if "american_express"

puts " Your debt for this card is 500 dollars do you wish to pay it? if yes please enter amount\n "

answer = gets.chomp.to_i if answer == 500

puts "\nYou just paid off american express. "

else

puts "Sorry you still owe us money "

end end

if "visa"

puts "Your debt for this card is 900 dollars do you wish to pay it? if yes please enter amount\n "

answer = gets.chomp.to_i if answer == 900

puts "\nYou just paid off for Visa. "

else

puts "Sorry you still owe us money " end end

if "Mickey_mouse_club" puts "Your debt for this card is 300 dollars do you wish to pay it? If yes please enter amount/n"

answer = gets.chomp if answer == 300 puts "You just paid off this card" else puts "Still owe us" end end end

SO i know this is all over the place. The goal here is to make a choice about paying off a credit card. I only have the paying part down so far. So what keeps happening is after i choose a card and enter the amount to pay it then goes on to the next card in the hash. Like its iterating over the whole hash. I want it to stop after i pay of one card.

1 Answer

Chris McKnight
Chris McKnight
11,045 Points

You should really make your program into a loop asking the user for input until they choose an option to exit.

To make your current code not continue to the next card, you need to fix the if statements. You currently are using the if statements on string literals. You need to compare the value of the string with the credit_card variable.

if "american_express" == credit_card
  puts "American Express"
end