What's new

Help Pahelp nman po!! Ruby Programming?

plasmadash

Addict
Joined
May 19, 2016
Posts
53
Reaction
11
Points
77
Write a while loop that takes input from
the user, performs an action, and only
stops when the user types "STOP". Each
loop can get info from the user.
 
Sure! Here's an example of a while loop in Ruby that takes input from the user until they type "STOP":

Code:
user_input = ""

while user_input != "STOP" do
  puts "Please enter some input (type STOP to exit):"
  user_input = gets.chomp
  # Perform some action with user_input
end

In this example, we first initialize the user_input variable to an empty string. Then, we enter a while loop that will continue until the value of user_input is "STOP".

Inside the loop, we prompt the user to enter some input using puts. We then use gets.chomp to read a line of input from the user, discarding the newline character at the end using the chomp method.

We can then perform some action with the value of user_input. In this example, I've left that part blank since I don't know what you want to do with the input.

Once the loop finishes, the program will continue with any code that comes after the loop.
 
Sure! Here's an example of a while loop in Ruby that takes input from the user until they type "STOP":

Code:
user_input = ""

while user_input != "STOP" do
  puts "Please enter some input (type STOP to exit):"
  user_input = gets.chomp
  # Perform some action with user_input
end

In this example, we first initialize the user_input variable to an empty string. Then, we enter a while loop that will continue until the value of user_input is "STOP".

Inside the loop, we prompt the user to enter some input using puts. We then use gets.chomp to read a line of input from the user, discarding the newline character at the end using the chomp method.

We can then perform some action with the value of user_input. In this example, I've left that part blank since I don't know what you want to do with the input.

Once the loop finishes, the program will continue with any code that comes after the loop.
thanks!!
 

Similar threads

Back
Top