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 trialAurelian Spodarec
7,369 PointsRuby adding list plus if else to them foreach of it
Hello, I want to be able to write a code when , if a man choose a color for his car eg green, this message will be displayed but before if he wants the collors , a list fo them will get on the screen.
How can I make it ?
puts " Whats your name ?"
name = gets.chomp
name.capitalize!
puts " Nice to meet you #{name}!"
puts " Would you like to continue (yes/no )?"
YeNo = gets.chomp
if YeNo == "yes"
puts " You will go throw "
else
puts " The program will stop now."
return nil
end
puts " Are you male or female?"
gender = gets.chomp
case gender
when "male"
puts " Choose your favorite color for your car:"
when "female"
puts " Choose one of the accessories: "
else
puts " Whoops, it looks like you didnt choose a gender!"
end
1 Answer
Maciej Czuchnowski
36,441 PointsThis sounds like exactly the same thing as you did with gender:
puts " Are you male or female?"
gender = gets.chomp
case gender
.......
List the colors, read the input from user and write case for each color you want to support.
Aurelian Spodarec
7,369 PointsI used the line break / n like that
puts " Whats your name ?"
name = gets.chomp
name.capitalize!
puts " Nice to meet you #{name}!"
puts " Would you like to continue (yes/no)?"
YeNo = gets.chomp
if YeNo == "yes"
puts " You will go throw "
else
puts " The program will stop now."
return nil
end
puts " Are you male or female?"
gender = gets.chomp
case gender
when "male"
puts " Choose your favorite color for your car:"
puts " Green \n Red \n Metalic \n Orange"
maleColor = gets.chomp
when "female"
puts " Choose one of the accessories:"
puts "Earings \n Jewellery \n Neclace \n Glasses "
femaleColor = gets.chomp
else
puts " Whoops, it looks like you didnt choose a gender!"
end
case maleColor
when "green"
puts "Green color is like grass."
when "red"
puts "Like a hell."
when "metalic"
puts " Robot color!"
when "orange"
puts "Orange to eat :D"
end
case femaleColor
when "earings"
puts "Put them on you ears."
when "jewellery"
puts "Good job to like tem !"
when "neclace"
puts "It should go on youru neck..."
when "glasses"
puts "Between nose on eyes."
end
if maleColor
puts " You choosed #{maleColor}, hope you like it."
elsif femaleColor
puts " You choosed #femaleColor, hope you engioj! "
else
puts " There was some error with your choosig."
end
puts " Do you want to reverse your name ( yes/no )?"
q2 = gets.chomp
if q2 == "yes"
puts " Enter your name you want to reverse . "
namRe = gets.chomp
namRe.reverse
puts " As you wish! Your name in lower case #{lowerNo}"
elsif q2 == "no"
puts " Maybe make it lower case ( lower / no )?"
lowerNo = gets.chomp
if lowerNo == "lower"
lowerNo.downcase!
elsif lowerNo == "no"
puts " I will not annoy you anymore"
else
puts " AS you want!"
end
else
puts " Something is wrong with , whatever.."
puts " The client will now exit"
end
Aurelian Spodarec
7,369 PointsAurelian Spodarec
7,369 PointsI just figured it out ;P