If, Else and ElseIf issue. Please help


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/learn-ruby/lessons/control-flow-in-ruby/exercises/if-else-and-elsif?action=lesson_resume&link_content_target=interstitial_lesson

<In what way does your code behave incorrectly? Include ALL error messages.>

```

puts "Type a letter of the Aphabet between A and B:"
gets.chomp

puts "Type another letter of the Aphabet between A and A:"
gets.chomp

a = 10
b = 11

if a < b
print “a is less than b!”

elsif b < a
print “b is less than a!”

else
print "b is equal to a!"
end

<do not remove the three backticks above>

#2

The instructions say nothing about gets.chomp.
Only use gets when you want the user to supply input.
I can explain more on gets if you’d like. In the mean time!

The instructions just want you to create an if/elsif/else statement

a = 10 # Set variable a to the value 10
b = 11 # Set variable b to the value 11

if a < b # if a is less than b do the following
  print "a is less than b!"
elsif b < a # if a wasn't less than b and b is less than a, do the following
  print "b is less than a!"
else # if none of those conditions were met
  print "b is equal to a!"
end # end the if statement

You could make up your own criteria, just as long as you have an if/elsif/else and it flows logically.

As I understand it, you want to let the user compare two values (a,b)
The system won’t know the values of a and b until the user inputs data

Thats when you would prompt the user like so

puts "Welcome to my number comparison program, enter two numbers to compare"
puts "Enter in the first number:" # prompt user to input a number
a = gets.chomp # assign the variable a to user input (while "chomping the newline character, dont worry too much about chomp for now!)

puts "Enter a number to compare to your first number:"
b = gets.chomp # assign the variable b to user input 

if a < b # if a is less than b do the following
  print "a is less than b!"
elsif b < a # if a wasn't less than b and b is less than a, do the following
  print "b is less than a!"
else # if none of those conditions were met
  print "b is equal to a!"
end # end the if statement

Notice that I never did the assignment of a and b. It wasn’t my job to do so. I want the user to give me those values, It is silly to ask the user to give you the value of a then assign it to ten.
Also note that you have to assign the value of gets to a variable or it will go nowhere!


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.