Reputation: 141
This is my first programming language so please bear with me!
I can't quite figure out where it's going wrong. I'm not necessarily asking for a solution as this is a learning exercise; I just need a helping hand as to where I should be looking.
#Calculate the sum of two numbers and an optional third
#get first number
print "Please enter your first digit: "
value_1 = gets.chomp
print value_1
#get second number
print "Please enter your second digit: "
value_2 = gets.chomp
#get the additional number
print "Do you want to add an additional number?"
add_num_req = gets.chomp
#calculate result and put
if gets.chomp = "yes" || "Yes" || "YES"
print "Please enter the additional digit: "
add_num_1 = gets.chomp
#print sum of three values
print "Answer: " , (value_1.to_i + value_2.to_i + add_num_1.to_i), "\n";
else
#print value_1 + value_2
print "Answer: " , (value_1.to_i + value_2.to_i), "\n";
end
But this produces a blank return after putting in the response to the get.chomp
for an additional digit. Any help?
Upvotes: 0
Views: 119
Reputation: 1281
My guess is your problem is happening here:
#get the additional number
print "Do you want to add an additional number?"
add_num_req = gets.chomp
#calculate result and put
if gets.chomp = "yes" || "Yes" || "YES"
Since you are calling gets.chomp
twice for the same input and using an assignment operator =
in place of comparison operator ==
. Also, as someone else has pointed out, each ||
operator should evaluate a Boolean expression, e.g. add_num_req == 'yes' || add_num_req == 'YES'
. Without modifying your code too much, I think you want something like this:
print "Do you want to add an additional number? "
add_num_req = gets.chomp
#calculate results and put
if add_num_req.downcase == 'yes'
# ...
On that note, if you plan to be evaluating a lot of strings, regular expressions are invaluable. I still can't write a decent regexp without checking a reference, but even so they make a world of difference!
Upvotes: 0
Reputation: 110725
Here's a more Ruby-like way to write your program:
def doit
value_1 = obtain_entry("Please enter your first digit: ").to_i
value_2 = obtain_entry("Please enter your second digit: ").to_i
loop do
case obtain_entry("Do you want to add an additional digit?: ")
when "yes", "Yes", "YES"
print "Please enter the additional digit: "
puts "Answer: #{value_1 + value_2 + gets.to_i}"
break
when "no", "No", "NO"
puts "Answer: #{value_1 + value_2}"
break
end
end
end
def obtain_entry(str)
print str
gets.chomp
end
doit
A few points:
obtain_entry
). For answers that are to be treated as integers, you may as well convert them to integers when they are returned. (In a real application you would of course want to make various checks on the type and reasonableness of answers.)"Do you want to add an additional digit?: "
and "Please enter the additional digit: "
."Do you want to add an additional digit?
. If an acceptable answer is given, we break out of the loop; if not, the question is repeated and the user is given another opportunity to answer.case
statement in the way I have done when several replies result in the same action being taken.#{variable_x}
), as in puts "Answer: #{value_1 + value_2 + gets.to_i}"
. You must use double quotes for string interpolation to work.gets.to_i
is sufficient.Upvotes: 0
Reputation: 36870
As a fourth alternative (and what I usually use) ...
if gets.chomp.downcase == "yes"
As with the regex match, it also accepts unexpected case arrangements (e.g. "yEs", "yES", "YeS" and so on)
Upvotes: 2
Reputation: 35600
A third alternative:
if ( ['yes','Yes','YES'].include?(add_num_req) )
...
Upvotes: 0
Reputation: 33380
In Ruby you can't compare a variable to many options as you have there. You have to do something like this:
if add_num_req == "yes" || add_num_req == "Yes" || add_num_req == "YES"
Another way to do it is to take advantage of the Enumerable module. But this is a little more advanced, although you will find it useful as you continue to use Ruby.
answers = ["yes", "Yes", "YES"]
if answers.any? { |e| add_num_req == e }
Upvotes: 1
Reputation: 9782
change:
if gets.chomp = "yes" || "Yes" || "YES" #you are using = instead of == which is equality
to:
if gets.chomp.match(/yes/i) #This is a case-insensitive regex to match "yes", "Yes" or "YES"
Upvotes: 0