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 , 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,441 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,441 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