8/8 What is wrong with my code?

Hi there!
I’m not native english speaker so it’s quite difficult to understand any sense of changing “Ce” to “The” etc.)) But it’s not the main issue. There’s my code below where I try to re-prompt the user for input if they don’t enter anything and replace “S” sounds…

print "What is your age?"
age=gets.chomp

print "What city are you from?"
city=gets.chomp
city.capitalize!

//Here I re-prompt the user for input if they don't enter anything
if age == "" || city == ""

    print "Please answer the questions"

        print "What is your age?"
        age=gets.chomp

        print "What city are you from?"
        city=gets.chomp
        city.capitalize!

else
    puts "I'm #{age}"
    puts "I'm from #{city}"
end

//Here I re-place 'S'-sounds
if city.include? "s"
    city.gsub!(/s/,th)

elsif city.include? "Ce"
    city.gsub!(/Ce/,"The")

else puts "There is no 'S' sounds in your string"
end

May I recommend that you simplify your working code to a basic proof of concept before complicating it with more code than necessary. That way you can work out the bugs.

Using backticks to format your code

Formatted code samples are easier to read and pick out the structure (indentation).

Couple of things to consider:

  • Repeat input when nothing entered:
begin
    print "Your input would be appreciated!"
    user_input = gets.chomp
end until user_input != ''
user_input.downcase!
  • Should you wish to be able to filter both the ‘s’ and the ‘ce’, don’t use elsif. Just write a second if statement.

A working proof of concept for you to model your project on…

begin
    print "Your input would be appreciated!"
    user_input = gets.chomp
end until user_input != ''
user_bak = user_input.dup
user_input.downcase!
if user_input.include? "s"
    user_input.gsub!(/s/, 'th')
else
    puts "#{user_bak} has no s's!"
end
if user_input.include? "ce"
    user_input.gsub!(/ce/, 'the')
else
    puts "#{user_bak} has no ce's!"
end

All I did was fashion it to just meet the suggested criteria.

Thank you for the assist!

I don’t think you actually need a second if statement

if (user_input.include? “s”) ||(user_input.include? “ce”)
user_input.gsub!(/s/, “th”)
user_input.gsub!(/ce/, “the”)

Not sure if this is correct, but it’s working…

1 Like