Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

If else etc ... not working

Hello What is wrong with this code ? i tried cople of things but it doestn work. I want to make if the user input is male then output this , if is female output the other.

print "What is your name ?"
name = gets.chomp
name.capitalize!
puts "Hello #{name}!"

print " Are you male or female?"
gender = gets.chomp

if gender == "male"
    print = " I have a car for you"
  elsif gender == "female"
    print = " I hae clothes for you"
end

2 Answers

Nathan Williams
seal-mask
.a{fill-rule:evenodd;}techdegree
Nathan Williams
Python Web Development Techdegree Student 6,851 Points

Hi Aurelian,

The reason the result isn't printing is that you're assigning it as a variable, rather than calling the print method.

And for what it's worth, the way I'd generally see something like this done is with a case statement:

puts "What is your name ?"
name = gets.chomp
name.capitalize!
puts "Hello #{name}!"

puts " Are you male or female?"
gender = gets.chomp

case gender
when "male"
  puts " I have a car for you"
when "female"
  puts " I have clothes for you"
else
  puts " Woops, we don't have anything for #{gender}"
end

AA i remember that on basics or somewhere in the ruby courses there was print the questoin and puts as output . I see here the code you made , uh well i dont remember learning case and when but ii know what it does apart from case . Thank you :) im trying to get my self amiliar to write this sort of different programs as a practice so i can move to next course and have everything easier . Thank you you all :) i look at this code.

You're doing print =, which is assigning the string to the variable "print". Just leave out the equals and it should work :)

Also, your elsif statement doesn't have to be indented like that. In fact, it might be better if it's not, since indenting it might make it a nested elsif.