03/19


#1



I am getting an infinite loop error when I inputted my own code for this lesson wanting to know if there is anything wrong with this approach I took.

def greeting
  puts "Hello what is your name?"
  user_input = gets.chomp
    while user_input.length == 0
     print "Please tell me your name."
     user_input = gets.chomp
end
puts "#{user_input}, well that is a very pretty name!"
end


#2

you need to escape the while loop with a break
while something == something
print "hello"
break
end
otherwise you get stuck in the loop


#3

def greeting
puts "Hello what is your name?"
user_input = gets.chomp
while user_input.length == 0
print "Please tell me your name."
user_input = gets.chomp
break if user_input.length >= 1
end
puts "#{user_input}, well that is a very pretty name!"
end

So I changed my code to this and the following happens:

  1. If there is no input the program loops and continues to asks the end user to input some text which is what I want

  2. If the user inputs text then the system outputs: #{user_input}, well that is a very pretty name! , but the console is still telling me that the way it is written will have an infinite loop

Is there an issue with my code or perhaps another way to write the program.


#4

haha I just realized the wrong character was used for equals to or greater than 1

should be
break if user_input.length <= 1
and not
break if user_input.length >= 1


#5

hmm well now when i press enter twice without inputting something it prints out

" , well that is a very pretty name!"


#6

sorry I poorly constructed it the other day when I tested you won't need a break. Once you remove it it should function properly


#7

You will need to call your method you have created though. So after the end of your method definition call
greeting


#8

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