0
Begin/Retry code error. Why?
I want the code to repeat from the "begin" when the "if" gets the "else" statement. It gives me ..\Playground\: ..\Playground\:24: Invalid retry (SyntaxError) THANKS! # Creates input method def input x = gets.to_i end #Begins the first choice begin puts "What will you do? 1- Get a closer look 2- Go in the opposite direction Write your input an press enter:" #Calls input method choice = input if choice == 1 puts "You get a closer look and..." elsif choice == 2 puts "You go in the opposite direction, out of trouble" else puts "Incorrect input, enter a number between the one's avaliables" end #Retries if the choice is error retry if choice != 1||2 end
2 ответов
0
The "retry" line looks wrong. You wanted to retry if the choice is not equal to1 and choice is not equal to 2, right? That if statement won't do that.
0
Exactly, so, How can I do it?
Well, someone answered me in StackOverflow. I paste it
# Creates input method UNUSED
def input
x = gets.to_i
end
loop do
puts "What will you do?",
"1- Get a closer look",
"2- Go in the opposite direction",
"Write your input and press enter:"
choice = gets.to_i
if choice == 1
puts "You get a closer look and..."
break
elsif choice == 2
puts "You go in the opposite direction, out of trouble"
break
else
puts "Incorrect input, enter a number between the ones available:"
end
end