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 trialNicolas Hémidy
8,481 PointsDefinition of variables - Arrays
Hello everyone,
I think i misunderstood the whole process of :
- how define a variable
- how use it in a array
- how call a method than use the variable of the array
Here is my code :
def q1()
print "Please tell me your name : "
end
def q2()
print "Do you want to try a little quiz ? (yes/no): "
end
def q3()
print "Here is my first question! What is my best french movie?: "
end
def r1()
print "What a lovely name !"
end
def r2()
print "Ok, nice to meet you "
end
def r3()
print "Correct!"
end
def r4()
print "Brilliant!"
end
def r5()
print "Wrong.."
end
a1 = "julie"
questionvar = [q1,q2,q3]
resultvar = [r1,r2,r3,r4,r5]
answervar = [a1]
def question(question, answer, result1, result2)
answeruser = gets.chomp.downcase
if answer.include? answeruser
result1
else
result2
end
end
question(questionvar[0], answervar[0], resultvar[0], resultvar[1])
Could you please explain me what is wrong ? I really thank you in advance, Nicolas
2 Answers
Luke Fritz
10,255 PointsThe code in your comment should run.
But you will get some odd behavior for your conditional. gets() returns a string, so include?() will be true even if only part of their answer matches, like 'juliet' or 'blahnicolasblah'.
Here is a similar example showing creating an array with a nested array. Also, I switched include?() to ==.
questions = [
"Please tell me your name : ",
"Do you want to try a little quiz ? (yes/no): "
]
answers = [
["julie", "nicolas"],
["yes", "no"]
]
results = [
["What a lovely name ! ", "What a great name! ", "Ok, nice to meet you "],
["Yeahhh, let's go! ", "Ok, your choice :)", "Sorry, I did not understand"]
]
def question(question, answers, result)
print question
answeruser = gets.chomp.downcase
if answeruser == answers[0]
print result[0]
elsif answeruser == answers[1]
print result[1]
else
print result[2]
end
end
question(questions[0], answers[0], results[0])
question(questions[1], answers[1], results[1])
Nicolas Hémidy
8,481 PointsHi Luke !
I really thank you for your answer. I used nested arrays to improve my quiz
questions = [
"Please tell me your name : ", "Do you want to try a little quiz ? (yes/no): ", "Here is my first question! What is my best french movie?: ", "Here is my second question! What is my favorite entrepreneur?: "
]
answers = [
["julie", "nicolas"],
["yes", "no"],
["le premier jour du reste de ta vie"],
["steve jobs"]
]
results = [
["What a lovely name ! ", "What a great name! ", "Ok, nice to meet you "],
["Yeahhh, let's go! ", "Ok, your choice :)", "Sorry, I did not understand"],
["Correct! ", "Wrong.. ", "Sorry, I did not understand"]
]
def question(question, answers, result)
print question
answeruser = gets.chomp.downcase
if answeruser == answers[0]
puts result[0]
elsif answeruser == answers[1]
puts result[1]
else
puts result[2]
end
end
question(questions[0], answers[0], results[0])
question(questions[1], answers[1], results[1])
question(questions[2], answers[2], results[2])
question(questions[3], answers[3], results[2])
I just have one question, why did you add an x as the fourth argument of the method question ? def question(question, answers, result, x)
Thank you in advance, Nicolas
Luke Fritz
10,255 PointsYeah, that looks good. I like that there is a relationship between the separate pieces of data (the same index in each array). It feels easier to read.
You get to start doing some really cool stuff once you get into creating classes.
Oops, I forgot to delete the x. It was part of some debugging code I had included so I wouldn't have to key in the user guess each time. I had deleted the other stuff. I'll update my answer.
Nicolas Hémidy
8,481 PointsNicolas Hémidy
8,481 PointsOk, I think that I found the solution.
If someone can confirm that I am on the right track, could be great :)
Thank you, Nicolas