hello world!
i’m a beginner at Ruby language, and i’m facing a problem for which i
fail to find the solution.
I will put a big block of code since I don’t know where the error comes
from…
The problem is that when I answer something else than “yes” or “no” to
the question “Finished?”, the program stops the while loop and goes to
the next block of code. Instead of stopping it, it should ask me again
to put “yes” or “no”, until I put “yes” or “no”.
I also join a screenshot of my code so you can see it better. (note: in
this post, i’ve deleted some useless part of code and traducted some
comments from french to english)
Here is my code:
list = Array.new
finished = “no”
#create a hash in which the values are lists of values, so i can have
keywords corresponding to authors, and lists of values corresponding to
the lists of files created by each author
hash = Hash.new do |hsh, key|
hsh[key] = []
end
while finished == “no”
puts “What file would you like to implement?”
file = gets.chomp
time = Time.now
puts “Who’s the author?”
author = gets.chomp
if hash[author].include? file
puts "There already is a file named \"#{file}\" corresponding to
the author “#{author}”."
#gives a value to the value-list of a key
else hash[author].push(file)
end
puts "\nFinished? yes/no"
finished = gets.chomp
finished.downcase!
puts ""
#here, whenever i give the variable finished another value than
“yes” or “no”, it should ask again the user to put a value in the
variable finished, until the value given is “yes” or “no”
case finished
when finished == “”
finished = gets.chomp
finished.downcase!
when finished != “yes” && finished != “no” && finished != “”
puts “Put “yes” or “no” please!”
finished = gets.chomp
finished.downcase!
end
end