0

I create an array from a text file which contains the english irregular verbs. I want the code to ask me the verbs in random order letting me proceed only if I respond correctly. I need to compare a string with an element of array. I wrote this:

a = []
File.open('documents/programmi_test/verbi.txt') do |f|
  f.lines.each do |line|
    a <<line.split.map(&:to_s)
  end
end
puts ''
b = rand(3)
puts a[b][0]
puts 'infinitive'
infinitive = gets.chomp
  if infinitive = a[b][1]    #--> write like this, I receive alway "true"
    puts 'simple past'
  else
    puts 'retry'
  end
pastsimple = gets.chomp
  if pastsimple == a[b][2]    #--> write like this, I receive alway "false"
    puts 'past participle'
  else
    puts 'retry'
  end
pastpart = gets.chomp
  if pastpart == a[b][3]
    puts 'compliments'
  else
    puts 'oh, no'
  end

can somebody help me?

2
  • Please provide a better expected result to go by, and have you tried anything? If so please provide that as well. Commented Nov 4, 2017 at 19:04
  • If the code asks to me to write the infinitive of the verb "battere" (position [0] of the array [b], in the text file it's the position [0] of the array [1]), I have to write "beat" (position [1] of the array [1]) to go on. If I write something else, I have to try again. Commented Nov 4, 2017 at 19:41

1 Answer 1

1

if infinitive = a[b][1] is assigning to inifinitive the value of a[b][1], unlike pastsimple == a[b][2] that's a comparation between both values.

You could try replacing the = for ==.

a = []

File.open('documents/programmi_test/verbi.txt') do |file|
  file.lines.each do |line|
    a << line.split.map(&:to_s)
  end
end

puts ''
b = rand(3)
puts a[b][0]
puts 'infinitive'

infinitive = gets.chomp
puts infinitive == a[b][1] ? 'simple past' : 'retry'
pastsimple = gets.chomp
puts pastsimple == a[b][2] ? 'past participle' : 'retry'
pastpart = gets.chomp
puts pastpart == a[b][3] ? 'compliments' : 'oh, no'
Sign up to request clarification or add additional context in comments.

2 Comments

I think the problem is that the code doesn't compare the "infinitive" with the string in the file that is in the position [1] of the [b] (random) array
You need to debug such values, using byebug you can check them easily.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.