I am trying to make a program to guess the job the user is thinking of, I have become stuck on a point to do with the updating of the database after the program has made an incorrect guess. I use a variable to store the stage at which the user is, and am trying to use it to replace the value that it refers to. I've simplified the program and pasted it below:
我正在尝试制作一个程序来猜测用户正在考虑的工作,在程序做出不正确的猜测之后,我已经陷入了与更新数据库有关的问题。我使用一个变量来存储用户所在的阶段,并尝试使用它来替换它所引用的值。我简化了程序并粘贴在下面:
@question_database = {'Does it need training?' => 'Mechanic' , 'Is it to do with sports?' => {'Is it to do with teaching?' => 'P.E. Teacher', 'Is it competitive?' => 'Athlete'} }
@question_stage = @question_database['Does it need training?']
@job = 'Mechanic'
puts "What is a question that could identify a #{@job}?"
primary_answer = @job
primary_question = gets.chomp
puts 'Which job were you thinking of?'
secondary_answer = gets.chomp
puts 'What is a question that could identify it?'
secondary_question = gets.chomp
@question_stage={ primary_question => primary_answer, secondary_question => secondary_answer}
It doesn't seem to do anything at all to the database. Does anyone know what I should do to fix this? Thanks, Ben
它似乎根本不对数据库做任何事情。有谁知道我应该怎么做才能解决这个问题?谢谢,本
1 个解决方案
#1
0
When you assign a new value to @question_stage
you don't change the value of @question_database['Does it need training?']
为@question_stage分配新值时,不要更改@question_database的值['是否需要培训?']
You might want to write your code like this:
您可能想要编写如下代码:
@question_database = {'Does it need training?' => 'Mechanic' ,
'Is it to do with sports?' =>
{'Is it to do with teaching?' => 'P.E. Teacher',
'Is it competitive?' => 'Athlete'} }
@job = 'Mechanic'
puts "What is a question that could identify a #{@job}?"
primary_answer = @job
primary_question = gets.chomp
puts 'Which job were you thinking of?'
secondary_answer = gets.chomp
puts 'What is a question that could identify it?'
secondary_question = gets.chomp
@question_database['Does it need training?'] = { primary_question => primary_answer,
secondary_question => secondary answer }
That is, of course, if I understand what you were trying to do, anyway to change the @question_database
you need to assign to one of its keys, or change one of its values.
也就是说,当然,如果我理解你要做什么,无论如何要更改@question_database,你需要分配给它的一个键,或者改变它的一个值。
#1
0
When you assign a new value to @question_stage
you don't change the value of @question_database['Does it need training?']
为@question_stage分配新值时,不要更改@question_database的值['是否需要培训?']
You might want to write your code like this:
您可能想要编写如下代码:
@question_database = {'Does it need training?' => 'Mechanic' ,
'Is it to do with sports?' =>
{'Is it to do with teaching?' => 'P.E. Teacher',
'Is it competitive?' => 'Athlete'} }
@job = 'Mechanic'
puts "What is a question that could identify a #{@job}?"
primary_answer = @job
primary_question = gets.chomp
puts 'Which job were you thinking of?'
secondary_answer = gets.chomp
puts 'What is a question that could identify it?'
secondary_question = gets.chomp
@question_database['Does it need training?'] = { primary_question => primary_answer,
secondary_question => secondary answer }
That is, of course, if I understand what you were trying to do, anyway to change the @question_database
you need to assign to one of its keys, or change one of its values.
也就是说,当然,如果我理解你要做什么,无论如何要更改@question_database,你需要分配给它的一个键,或者改变它的一个值。