I am attempting to make a Vector Calculator, that will take either
coordinates, or an already given vector. The if and elsif blocks work
fine, it is the else block that I am in need of help in. I want the code
to re-run from the "answer = gets.chomp.to_i" line if the answer does
not equate to "given" or "coordinates", but when i run the code I am
given the following errors:
vector_calc.rb:48: warning: string literal in condition
vector_calc.rb:51: warning: string literal in condition
vector_calc.rb:50: Invalid retry
vector_calc.rb: compile error (SyntaxError)
The code is below.
puts "Are the vectors given, or do you only know coordinates?"
puts "Please answer either 'given' or 'coordinates'."
begin
answer = gets.chomp.downcase
if answer == "coordinates"
puts "What is the x-value of your first coordinate?"
x1 = gets.chomp.to_i
puts "What is the y-value of your first coordinate?"
y1 = gets.chomp.to_i
puts "What is the x-value of your second coordinate?"
x2 = gets.chomp.to_i
puts "What is the y-value of your second coordinate?"
y2 = gets.chomp.to_i
coord_1 = [x1, y1]
coord_2 = [x2, y2]
puts "Point A = (#{x1}, #{y1})"
puts "Point B = (#{x2}, #{y2})"
elsif answer == "given"
puts "What is vector A's x-value?"
vect_Ax = gets.chomp.to_i
puts "what is vector A's y-value?"
vect_Ay = gets.chomp.to_i
vect_A = [vect_Ax, vect_Ay]
puts "What is vector B's x-value?"
vect_Bx = gets.chomp.to_i
puts "what is vector B's y-value?"
vect_By = gets.chomp.to_i
vect_B = [vect_Bx, vect_By]
puts "Vector A = [#{vect_Ax}, #{vect_Ay}]"
puts "Vector B = [#{vect_Bx}, #{vect_By}]"
else
if answer != "given" || "coordinates"
then puts "Please answer either 'given' or 'coordinates'."
end
until answer == "coordinates" || "given"
retry
end
end
end
P.S I am creating this code as part of an outcome to a research project,
and am asking your permission to use your answer to this question as
evidence to my folio.
on 2012-12-05 07:27
on 2012-12-05 10:00
On Wed, Dec 5, 2012 at 7:27 AM, Nuggety Nanna <lists@ruby-forum.com> wrote: > vector_calc.rb: compile error (SyntaxError) > puts "What is the x-value of your first coordinate?" > > puts "what is vector A's y-value?" > vect_B = [vect_Bx, vect_By] > end > end The conditions are wrong: 1.9.2p290 :001 > answer = "x" => "x" 1.9.2p290 :002 > if answer != "coordinates" || "given" 1.9.2p290 :003?> puts "a" 1.9.2p290 :004?> end (irb):4: warning: string literal in condition a A string literal will always evaluate to true in a boolean comparison: if answer != "coordinates" || answer != "given" Now, the || (or) is wrong, because any word is either != "coordinates" or != "give". You want && (and): if answer != "coordinates" && answer != "given" But, as you have this comparison in an else of an if that is comparing answer to coordinates and given, it's not even needed. Anyway, I think it would be simpler if you moved this logic to the beggining of your code: begin puts "Please enter coordinates or given" answer = gets.chomp.downcase end until answer == "coordinates" || answer == "given" # at this point you know that answer is either coordinates or given if answer == "coordinates" [...] > P.S I am creating this code as part of an outcome to a research project, > and am asking your permission to use your answer to this question as > evidence to my folio. NP. Jesus.
on 2012-12-05 10:08
answer = 'hello'
if answer != "given" || "coordinates"
puts "Please answer either 'given' or 'coordinates'."
end
--output:--
1.rb:5: warning: string literal in condition
1)
answer = 'hello'
if answer != "given" and answer != "coordinates"
puts "Please answer either 'given' or 'coordinates'."
end
2)
answer = 'hello'
if not ["given","coordinates"].include?(answer)
puts "Please answer either 'given' or 'coordinates'."
end
============
begin
answer = gets.chomp.downcase
until answer == "coordinates"
retry
end
end
--output:--
1.rb:5: Invalid retry
1.rb: compile error (SyntaxError)
You are trying to learn ruby by skipping the basics--that can't be done.
on 2012-12-05 10:43
Shouldn't this: if answer != "given" || "coordinates" until answer == "coordinates" || "given" Be this? if answer != "given" && answer != "coordinates" until answer == "coordinates" || answer == "given" And your "retry" must be in the wrong place or context, doesn't it have to be inside a "rescue"?
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.