Not getting expected answer [closed] - ruby

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Code not returning the correct answer.
I've tired assigning a value to the animal choices. I've put it in the def and outside of it.
puts "Choose your favorite: cats or dogs"
choose = gets
cats = 1
dogs = 2
def favorite_animal (number)
remainder_when_divided_by_2 = number % 2
if remainder_when_divided_by_2 == 0
return "Ken does too."
end
if remainder_when_divided_by_2 == 1
return "Dogs are better!"
end
end
If the user enters Cats the answer "Ken does too!" should show. If the user enters Dogs the answer "Dogs are better!" should show. All I've gotten is 1 or 2 as an answer.

Try this.
loop do
puts "Choose your favorite: cats or dogs"
case gets.chomp
when "cats"
break "Ken does too."
when "dogs"
break "Dogs are better!"
else
puts "That answer is invalid. Try again"
end
end
Here is an example of a session using this code, with my answers being "pigs" and "dogs".
Choose your favorite: cats or dogs
pigs
That answer is invalid. Try again
Choose your favorite: cats or dogs
dogs
#=> "Dogs are better!"
See Kernel#loop. Many Rubyists use loop with the keyword break for most loops, rather than while or until. (for loops are never used).
For what you are doing you don't need a method, but if you want one add the line
def favorite_animal
at the beginning and the line
end
at the end. Then
favorite_animal
#=> "Dogs are better!"
provided I were to give the same answers as I did earlier.

There's a couple things going on:
You have to call the method favorite_animal somewhere; you've only defined it
Your cats/dogs isn't "mapped" to anything, so you need some logic to convert your input into a number, before you call the favorite_animal method
You still have to do something with the value you return inside your method (puts or something else to get it to show)
Here's a minimum example that works that might be useful for you to see the 3 issues above
def favorite_animal (number)
remainder_when_divided_by_2 = number % 2
if remainder_when_divided_by_2 == 0
return "Ken does too."
end
if remainder_when_divided_by_2 == 1
return "Dogs are better!"
end
end
puts "Choose your favorite: cats or dogs"
choose = gets.chomp
answer = if choose == 'cats'
1
else
2
end
puts favorite_animal(answer)

Related

Am I doing this right? If not could someone please explain how it's supposed to work? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Hello i would like to know what wrong with my code
pizza = "2.99"
puts "hello what would you like to buy?"
food_type = gets.chomp.downcase
puts "How many?"
food_amount = gets.chomp!
puts #{food_amount.to_i * food_type.to_f}
To do something like this, you should store the different types of food in hash, and with { "food type" => cost of food } (for example, { 'pizza' => 9.99, 'soda' => 2.99, 'breadsticks' => 1.50 }) Note the prices are already floats, so no conversion is needed later.
Then when you loop through, you can save what they buy in another hash { 'food type' => quantity }, with the quantity being an integer. Then, when they finish selecting items, you loop through the 'cart' and multiply everything out:
menu = { 'pizza' => 9.99, 'soda' => 2.99, 'breadsticks' => 1.50 }
cart = Hash.new(0)
begin
puts "hello what would you like to buy?"
food_type = gets.chomp.downcase
puts "How many?"
quantity = gets.to_i
cart[food_type] += quantity
puts "Want Anything Else?"
buy_more = (gets.chomp == "yes")
end while buy_more
puts "you're buying:"
cart.each do |food_type, quantity|
puts "#{quantity} of #{food_type} for #{quantity * menu.fetch(food_type, 0)}"
end
Example Run:
hello what would you like to buy?
soda
How many?
2
Want Anything Else?
yes
hello what would you like to buy?
pizza
How many?
5
Want Anything Else?
yes
hello what would you like to buy?
breadsticks
How many?
1
Want Anything Else?
yes
hello what would you like to buy?
something else
How many?
10
Want Anything Else?
no
you're buying:
2 of soda for 5.98
5 of pizza for 49.95
1 of breadsticks for 1.5
10 of something else for 0
Read more about Hash in the docs, which should have enough info to get you going on the parts of your repl that I haven't added and decide how you'd like to handle invalid entries (like when I entered 'something else' for a food type). This example just treats it as a valid item with no price.

Ruby Loop Countdown method not Counting down [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm trying to define a method that countdowns 10 to 0 and at the end returns HAPPY NEW YEARS! however i don't want i"am doing wrong?
def countdown(number)
while number > 0
puts "#{number} SECONDS(S)!"
number -= 1
end
"HAPPY NEW YEAR!"
end
A quick Google search revealed that you are apparently trying to solve https://learn.co/lessons/countdown-to-midnight (you really should have included that link)
Your code is not passing the spec, because it contains an additional S:
puts "#{number} SECONDS(S)!"
# ^
It has to be:
puts "#{number} SECOND(S)!"
def countdown(number)
while number > 0
puts "#{number} SECONDS(S)!"
number -= 1
end
puts "HAPPY NEW YEAR!"
end
I added a puts on the last line of your code. You method is counting down to 0 seconds, but the last line only return the string "HAPPY NEW YEAR!", and does not print it to the screen. Or if you need to return the string and print it on the screen, you can do p "HAPPY NEW YEAR!"

How to solve ruby string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm new to ruby, and I'm trying to make a simple calculator in which a user types in a simple problem (such as addition or subtraction) and the answer is returned. The problem is when the user types in a question, the question itself is being returned instead of the answer to that question.
puts "How many Questions?"
questions = gets.chomp.to_i
questions.times do |problem|
puts "question: "
problem = gets.chomp
puts "answer: #{problem}"
end
Inside your loop, instead of:
problem = gets.chomp
puts "answer: #{problem}"
Try this:
problem = gets.chomp
solved_problem = eval(problem)
puts "answer : #{solved_problem}"
eval will take care of interpreting your string as a Ruby instruction. But it's very messy, because anyone could write any Ruby program in your input and eval will make it run, so it's not safe at all.
If you only want to take care of simple operations, you can use a regex first to check if the input string looks like what you want:
problem_is_ok = (problem =~ /^\d+ *[+-] *\d+$/)
This will be true if the string starts with a number, followed by 0 to many spaces, followed by either a + or - sign, followed by 0 or more spaces, followed by another number and nothing else. Then you raise an error if this is not true.
Your loop now look like this:
questions.times do |problem|
puts "question: "
problem = gets.chomp
problem_is_ok = (problem =~ /^\d+ *[+-] *\d+$/)
if problem_is_ok
puts "answer: #{eval(problem)}"
else
#I raise an error, but you might aswell just print it instead for your simple program
raise ArgumentError("Can't compute this")
end
end
Add and subtract can be simple ruby definitions
We pass in 5 and 1
The lower portion of the code is the clear user interface implementation
when we loop we do it 3 times
It outputs 3 options for the user to select from
We read in with chomp, standard in, the keyboard, chooses 1, 2, or 3
If elsif statements conditionally select for each case
Using string interpolation we render the variables a and b into a new string,
and run their respective methods (add or subtract)
Converting that methods integer output to a string, and concatenate it.
Outputting that to the users screen
The 3rd option does no calculation,
instead it prints to users screen a simple string
our else condition catches the case when people don't enter one of the choices of 1, 2 or 3
It tells you to correct your choice to the options provided
Hope this helps
#!/usr/bin/env ruby
questions = 3
a, b = 5, 1
def add(a,b)
a + b
end
def subtract(a,b)
a - b
end
questions.times do |questions|
puts "Please choose:
1. add
2. subtract
3. exit"
questions = gets.chomp
if questions == '1'
puts "#{a} + #{b} = " + add(a,b).to_s
elsif questions == '2'
puts "#{a} - #{b} = " + subtract(a,b).to_s
elsif questions == '3'
puts 'exiting, goodbye.'
exit
else
p 'please choose again'
end
end

Can I insert an `if` condition inside a `case` statement? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to build a game. I want to give the users options depending on what they choose. My code has to be in a case statement:
puts "HA! you are in Saw 18"
puts "Lets play a deadly game! >=D"
puts "Options: hight, Who is Superman, Bake a pie "
option = gets.chomp
case option
when "hight"
puts "Lets see your hight yo!"
option_1 = gets.chomp
case option_1
when
end
end
If they choose the Hight,
if i >= 5 ft
puts "you may live"
else
puts "you in trouble"
Any other alternatives using case are very welcome.
Yes, you can insert a condition inside the case statement.
You could call a function from the case statement and put the if condition inside the function. Unless you have a good reason not to do this?
Yes you can, I suppose (trying not to change your code too much) you could do something like this:
puts "HA! you are in Saw 18"
puts "Lets play a deadly game! >=D"
puts "Options are: height, Who is Superman or Bake a pie "
option = gets.chomp
case option
when "height"
puts "Lets see your height yo!"
option_1 = gets.chomp.to_i
if option_1 >= 5
puts "you may live"
else
puts "you're in trouble"
end
when "Who is Superman"
#code
else
#code
end

99 bottles of beer on the wall in Ruby [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying to write a Ruby loop for the song "99 Bottles of Beer On The Wall" for an exercise from the book "Learn To Programs". What am I doing wrong? I have the following:
def bottles_of_beer
i = 99
while i < 99 and i > 0
puts "#{a} bottles of beer on the wall. #{a} bottle of beer."
i = i - 1
puts "Take one down, pass it around. #{i} bottle of beer on the wall."
end
end
You are referencing undefined variable a in your first string.
I have simplified your code by quite a bit:
i = 99
while i < 99 and (anything else)
(anything)
end
Try and see if you can figure it out now.
TL;DR
You have many problems with your code, not least of which i starts out equal to 99, so the rest of the code block is never evaluated. Even if you fix that, a will always be nil because you never assign anything to it.
Fix Your Conditional
There are many ways to do this, but you probably want to use the >= or <= methods for your comparisons.
Be More Idiomatic
Using Integer#downto and a block would be much more idiomatic. For example:
12.downto(1) { |count| p "#{count} bottles of beer on the wall..." }
p "You drank the whole case!"
Perhaps...
99.downto(1) do |i|
puts "#{i} bottle#{i==1 ? '' : 's'} of beer on the wall, #{i} bottle#{i==1 ? '' : 's'} of beer!"
puts "Take one down, pass it around, #{i-1} bottle#{i-1==1 ? '' : 's'} of beer on the wall!"
end
To give you a definitive answer, there are three reasons why your code produces no output
You set i to 99 and then loop while i < 99 and i > 0, so the loop is never executed. Since you are always decrementing i, there is no need for anything more than while i > 0
You interpolate the variable a into the string you are printing. Since you haven't declared it your program will refuse to run, saying undefined local variable or method 'a'
You never actually call your method.
Fixing these three problems gives this (non-idiomatic, but working) program
def bottles_of_beer
i = 99
while i > 0
puts "#{i} bottles of beer on the wall. #{i} bottle of beer."
i = i - 1
puts "Take one down, pass it around. #{i} bottle of beer on the wall."
end
end
bottles_of_beer

Resources