Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby

If , else , when, case etc..

Hello,

When i get throw half the code , it breaks im not sure whats the right think to do .

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

print " 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

case male
when gender = male
  puts "How old are you?"
  age = gets.to_i
  if age >= 18
    puts " You are allowed to vote"
   end
case female
when gender = female
puts " Whats your date of birth?"
femaleAge = gets.to_i
if femaleAge >= 1996
  puts " You are allowed to enter"
end
end

2 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,440 Points

What David said, plus, some of your male and female are strings (in double quotes) and some are just variables (no double quotes). Like here:

when gender = male

I think you meant:

when "male"

Maciej Czuchnowski
Maciej Czuchnowski
36,440 Points

Also, the whole second case seems a bit off. case male suggests that you have a variable named male, but you don't, so it can't really work. Please read more on CASE in Ruby to understand it more thoroughly, you can start here:

http://www.skorks.com/2009/08/how-a-ruby-case-statement-works-and-what-you-can-do-with-it/

I changed it a bit but i have some error about male

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

print " 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

case male
  when "male"
    puts "How old are you?"
    age = gets.to_i
  if age >= 18
    puts " You are allowed to vote"
   end
case female
  when "female"
    puts " Whats your date of birth?"
    femaleAge = gets.to_i
  if femaleAge >= 1996
    puts " You are allowed to enter"
    end
  end
end

okay i check it now

J Scott Erickson
J Scott Erickson
11,883 Points

your case statements are using unassigned variables male and female.

I think what you actually want is this:

case gender
when "male"
  puts "How old are you?"
  age = gets.to_i
  if age >= 18
    puts "you are allowed to vote"
  end
when "female"
  puts "Whats your year of birth"   #Note, never ask a lady this :-P
  femaleAge = gets.to_i
  if femaleAge >= 1996
    puts "You are allowed to enter"
  end
end

thank you :) i see what wrong i did but i was close :smiley:
bdw i know not to ask a lady about her age but i though you can ask what year was she born in xd

Hi, I noticed that you're no longer comparing cases but assigning them at he bottom of your program.

when gender = male
when gender = female

// I'm guessing should be 

when gender == male
when gender == female

I changed it but i still get an error .
But thank you i did mess up a bit : p