Homework Help - Methods

R

Rick Barrett

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.
 
S

Seebs

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.
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.
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
 
7

7stud --

Rick said:
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()?
 
R

Rick Barrett

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...
 
7

7stud --

Rick said:
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?

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...

Once again, look at your method definition. How many parameter
variables are there in the get_subtotal definition? How many values did
you call the method with?

If you define a method like this:

def my_meth(a, b, c, d)
#do something
end

Then you MUST call my_meth with 4 arguments--not 3 arguments, and not 0
arguments. So you can never write:

my_meth

You have to call my_meth like this:

my_meth(10, 20, 5, 6)

See how there are 4 things between the parentheses in the method call?
Now look above and see how my_meth was defined with 4 variable names
between the parentheses? The values in the method call:

my_meth(10, 20, 5, 6)

namely 10, 20, 5, and 6 are called the "arguments". The variable names
in the method definition:

def my_meth(a, b, c, d)

namely a, b, c, and d are called the "parameter variables" or
"parameters". Guess what? When you call my_meth, ruby lines up the
method call with the method definition like this:


my_meth(10, 20, 5, 6)
def my_meth( a, b, c, d)

Then ruby assigns the values on top(the arguments) to the variables
underneath them(the parameter variables), like this:

a = 10
b = 20
c = 5
d = 6

Then inside the method if you write something like:

result = a * d
puts result

60 will be displayed.

How do I get the array "prices" into the get_subtotal method?

Look at this example:

def my_meth
prices = [2.5, 1.24, 6.80]
return prices
end

def show(arr)
str = arr.join(", ")
puts str
end


user_prices = my_meth
show(user_prices)
 
7

7stud --

7stud said:
Look at this example:

def my_meth
prices = [2.5, 1.24, 6.80]
return prices
end

def show(arr)
str = arr.join(", ")
puts str
end


user_prices = my_meth
show(user_prices)

One other thing, a method call in your code is replaced by the return
value of the method. So the call to my_meth in the code above will get
replaced by the return value of my_meth. So when you execute your
program, the line:

user_prices = my_meth

will become:

user_prices = [2.5, 1.24, 6.80]

If a method doesn't have a return statement, then the value of the last
expression that was executed will be returned. In the show() method,
the last expression that's executed is the puts statement, and puts
returns the value nil. You can alter the code to see that:

return_val = show(user_prices)
puts return_val

--output:--
nil



However, since the original example just says:

show(user_prices)


and a method call is replaced by its return value, and because show()
does not have a return statement and therefore the value of the last
expression is returned, and the last expression that is executed is
puts, the method call is replaced in the code with nil. So the line:

show(user_prices)

becomes:

nil

That may look strange having nil sitting on a line by itself, but you
can write a program like this:

puts "starting program"
10
nil
"hello"
puts "ending program"

--output:--
starting program
ending program

Values sitting on lines by themselves are simply discarded. That
program is equivalent to:

def meth1
return 10
end

def meth2
return nil
end

def meth3
return 'hello'
end

puts "starting program"
meth1
meth2
meth3
puts "ending program"

If you call a method and don't store the return value in a variable, the
return value is discarded. However, instead of this:

def my_meth
prices = [2.5, 1.24, 6.80]
return prices
end

def show(arr)
str = arr.join(", ")
puts str
end


user_prices = my_meth #<----*****this here
show(user_prices)

You could simply write:

show(my_meth)

The method call my_meth gets replaced in the code by its return value
which is the array [2.5, 1.24, 6.80]. Therefore, the line:

show(my_meth)

becomes:

show([2.5, 1.24, 6.80])

However, its clearer if you use two statements:

user_prices = my_meth
show(user_prices)

...and clarity trumps brevity every time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top