Working with loops


#1

Hello everyone,

So I made a script that will take 3 numbers from the user and provide
the sum and product of them. What I want to do is give the option for
the user to redo the process with a new set of number. It does restart
the process of asking for inputs but the problem is the variable is
already assigned from the previous run through (iteration?). Is there a
command to force the script to start from scratch and not carry over
previous inputs?

class Screen
def cls
puts ("\n" * 25)
puts “\a”
end

def pause
STDIN.gets
end

#Logic

Console_Screen = Screen.new
puts “Please enter a set of 3 numbers after pressing enter to get both
a product and sum.”

Console_Screen.pause
loop do
print 'First : ’
First = gets.to_i

print 'Second : ’
Second = gets.to_i

print 'Third : ’
Third = gets.to_i

sum = First + Second + Third
product = First * Second * Third
puts “Product: #{product}”
puts “Sum: #{sum}”
puts “Would you like another calculation?(y/n)”

answer = gets
answer.chop!

if answer == “y”

#--------------------------------------what i think the problem is
redo

#------------------------------------------------

else

puts “Have fun doing mental math.”
Console_Screen.pause
exit

end
end

end


#2

(1) You can encapsulate everything in a method or block, so that the
variables are local.

(2) You can explicitly assign nil to them in order to reset the
variables.