THITH MEANTH WAR! improved(?) by "unless" instead of second "if"


#1

Hello guys, at the end of “THITH MEANTH WAR!” is a challenge to improve the program by second if function. I tried to improve by using unless instead, but for some reason program stops after users input. Could anyone help pls?

user_input = ""
unless user_input.include? "s"
print "type some text including s: "
user_input = gets.chomp
else
user_input.gsub!(/s/, “th”)
puts "#{user_input}"
end

Thanks :slight_smile:


#2

because there is no more program after the user input… else sees to that.


#3

Thanks for your input :slight_smile:
for me as a beginner the program should behave like this:
variable set to ""
the program should ask for an input so many times, until user enters input which contains "s"
after receiving an input which contains “s” it should substitute it with “th” and print the result
could you please tell me what mistake i got there or how it should be written please?


#4

unless is a conditional, not an iterator. There is no loop in your code, only an if else. Try using until

unless
print "Your input would be appreciated! "
user_input = gets.chomp.downcase
unless user_input.include? "s"
  puts "Yes, there are no 's's"
else
  user_input.gsub!(/s/, "th")
  puts "Your string: #{user_input}"
end

This isn’t very elaborate but it gets the job done in terms of looping until the input contains an s

until
print "Your input would be appreciated! "
user_input = ""
until user_input.include? "s"
  user_input = gets.chomp.downcase
end
puts "Your string: #{user_input.gsub!(/s/, "th")}"

Congratulationth!


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.