| Computers Forum Index » Computer Languages (Ruby) » Homework Help - Methods... |
|
Page 1 of 1 |
|
| Author |
Message |
| Rick Barrett... |
Posted: Mon Nov 02, 2009 6:17 am |
|
|
|
Guest
|
The goal of this assignment is to ask the user for the price of each of
their items and print out a receipt using your own defined methods. I've
been doing well in the class(ahead actually) until the last two weeks
and I want to stay ahead.
This is what I have so far...
puts "Enter shopping cart item prices, one per line.\n"
puts "Press 0 when you are finished.\n"
items_in_cart = []
subtotal_cart = 0.0
subtotal_tax = 0.0
total_cost = 0.0
SALES_TAX = 0.07
def get_items_from_user()
items = []
item = ask_user_for_number("Enter item price:")
while (item != 0.0) do
items.push(item)
item = ask_user_for_number("Enter item price:")
end
return items
end
def ask_user_for_number(question_to_ask)
puts question_to_ask
response = gets().chomp().to_f()
end
def get_subtotal(cart_items)
subtotal = 0.0
cart_items.each() do |item|
subtotal = subtotal + item
end
return subtotal
end
def calculate_tax (cart_total)
return cart_total * SALES_TAX
end
puts get_items_from_user
puts ask_user_for_number
puts get_subtotal
puts calculate_tax
puts "Item subtotal: " + subtotal_cart.to_s()
puts "Sales Tax: " + subtotal_tax.to_s()
puts "Total: " + total_cost.to_s()
I keep getting this error
rb:43 in 'ask_user_for_number' : wrong number of arguments (0 for 1)
(Argument Error)
I have no idea what this means...and I don't see what's wrong in the
'ask_user_for_number' method so, I don't even know where to begin to fix
it.
Thanks in advance for any help.
--
Posted via http://www.ruby-forum.com/. |
|
|
| Back to top |
|
|
|
| Seebs... |
Posted: Mon Nov 02, 2009 6:17 am |
|
|
|
Guest
|
On 2009-11-02, Rick Barrett <chngth3wrld at (no spam) yahoo.com> wrote:
Quote: def ask_user_for_number(question_to_ask)
puts question_to_ask
response = gets().chomp().to_f()
end
You defined "ask_user_for_number" as a method taking an argument.
Quote: puts ask_user_for_number
Here, you call ask_user_for_number with no argument. Thus, it has
the wrong number of arguments. (0 for 1) means you gave it zero and
it needed 1.
Quote: I have no idea what this means...and I don't see what's wrong in the
'ask_user_for_number' method so, I don't even know where to begin to fix
it.
Nothing's wrong with it. What's wrong is the call to it down by the
puts later.
-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet-nospam at (no spam) seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated! |
|
|
| Back to top |
|
|
|
| Rick Barrett... |
Posted: Mon Nov 02, 2009 6:17 am |
|
|
|
Guest
|
def get_input(question_to_ask)
puts question_to_ask
input = gets().chomp().to_f()
end
def get_price()
prices = []
price = get_input("Enter item price: ")
while (price != 0.0) do
prices.push(price)
price = get_input("Enter item price: ")
end
return prices
end
So...basically get_input is inside of get_price...so there's no need to
call it down later in the code? Right?
Then there's the second half of the code...
def get_subtotal(prices)
subtotal = 0.0
prices.each do |price|
subtotal = subtotal + price
end
return subtotal
end
def get_tax(tax)
return tax * SALES_TAX
end
get_price
get_subtotal
get_tax
puts "Subtotal: $" + subtotal.to_s()
puts "Sales Tax: $" + tax.to_s()
puts "Total: $" + total.to_s()
I get the same error (0 for 1) (Argument Error) for get_subtotal...
How do I get the array "prices" into the get_subtotal method?
How do I get the input from the get_prices method to be added together
in the get_subtotal method?
And then how do I get it to print each of those if the variables stay
inside the methods?
I've searched the internet and read the method chapters we're on in my
book and I can't find the answer...or an explanation that I can
understand...for some reason I just can't get this...
--
Posted via http://www.ruby-forum.com/. |
|
|
| Back to top |
|
|
|
| 7stud --... |
Posted: Mon Nov 02, 2009 6:17 am |
|
|
|
Guest
|
Rick Barrett wrote:
Quote: The goal of this assignment is to ask the user for the price of each of
their items and print out a receipt using your own defined methods. I've
been doing well in the class(ahead actually) until the last two weeks
and I want to stay ahead.
This is what I have so far...
puts "Enter shopping cart item prices, one per line.\n"
puts "Press 0 when you are finished.\n"
...and why are those lines there instead of in the method
get_items_from_user() (which is a misnomer anyway because the method is
getting item prices not items, e.g. item = bread, item_price=$2.50)?
How about get_prices() and get_input() instead of get_items_from_user()
and ask_user_for_number()?
--
Posted via http://www.ruby-forum.com/. |
|
|
| Back to top |
|
|
|
|