Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Ask the user how much money they have, assume that the $ symbol is part of the prompt (the user doesn't have to enter it)
- # [Optional] Handle when the buyer enters "C" or "c" so that it works as expected
- # [Optional] Do something appropriate when the buyer enters an invalid amount for the money and an invalid selection
- puts "\n"
- puts "Hi kid! Welcome to my virtual candy machine. You give me virtual money, I give you virtual candy."
- puts "\n"
- print "How much money do you have? $"
- kid_money = gets.chomp.to_f
- puts "\n"
- # Since we're talking about money, format the response to include two decimal points
- puts "$%0.2f? Sweet!" % [kid_money]
- puts "\n"
- # Display all candy options and their costs (even if the user cannot afford the candy)
- puts "Okay, here's what I've got:"
- puts "\n"
- puts "a. Laffy Taffy, $0.10"
- puts "b. Bit o' Honey, $0.75"
- puts "c. Jelly Bellys, $1.75"
- puts "d. Kinder egg, $2.50"
- puts "\n"
- puts "So, which one do you want? Choose a letter."
- # Decide whether the user can afford the candy or not, if they can't, tell them so, if they can, calculate and display their change
- candy_option = gets.chomp.downcase
- puts "\n"
- # Use conditionals to define letter options
- if candy_option == "a"
- price = 0.10
- candy_name = "Laffy Taffy"
- elsif candy_option == "b"
- price = 0.75
- candy_name = "Bit o' Honey"
- elsif candy_option == "c"
- price = 1.75
- candy_name = "Jelly Bellys"
- elsif candy_option == "d"
- price = 2.50
- candy_name = "Kinder egg"
- else
- puts "Sorry, I don't recognize that choice. Here's your money back."
- puts "\n"
- end
- if kid_money < price
- puts "Get outta here, you don't have the money for that!"
- elsif kid_money == price
- puts "Okay, here's your #{candy_name}. Enjoy your virtual candy!"
- else kid_money > price
- change = kid_money - price
- puts "All righty, here's your #{candy_name} and your $%0.2f change. Enjoy!" % [change]
- end
- puts "Thanks for using the virtual candy machine."
- puts "\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement