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 trialAshmit Pathak
4,441 PointsI have tried a simple code to run a basic contact list app but it's not working plz tell me what's going wrong
def ask(question,kind = "string")
puts question + " "
answer = gets.chomp
answer = answer.to_i if kind == "number"
return answer
end
def get_contact
answer = ask("What is your name? ")
contact = {"name" => "","phone number"=>[]}
contact["name"] = answer
answer_1 = ""
while answer_1 != 'n'
number = ask("Do you want to add your phone number(y/n) ")
if number == 'y'
add = ask("Enter a phone number? ")
contact["phone number"].push(add)
end
return contact
end
contact_list = []
answer = ""
while answer != 'n'
contact_list.push(get_contact())
ask(Add Another?y/n)
end
puts '-'*80
contact_list.each do |contact|
puts "Name: #{contact["name"]}"
if contact["phone number"].size >0
contact["phone number"].each do |phone|
puts "Phone: #{phone}"
puts '_'*80
end
end
end
puts "-------\n"
end```
1 Answer
Brandon McClelland
4,645 PointsHi Ashmit,
Your variables cannot contain punctuation except for the underscore character "_", so a variable for "phone number"
is invalid. Try replacing it with phone_number
. Also don't use quotes around your variable names, the quotes are only for defining what is contained within a String. In you code every instance where you write "phone number"
Ruby will think you are trying to define a String, not a variable and this will greatly impact your code.
Good luck, Brandon