I am working on a lab for class about 'while' loops. This is the goal:
- Create a variable and set it equal to true
- While the variable you just created is true
- Prompt user for number
- Read in number
- Compute double that number
- Display double that number
- Prompt the user if they want to go again
- Read in that text
- If that text input is “n” then set the true variable to false
This is the code I have:
puts "Enter a number"
x = Float(gets.chomp())
product = x * 2
puts "Double your number is"
puts product
while
puts "Go again y/n?"
answer = gets.chomp
if answer != 'y'
end
end
The directions seem clear, but I don't understand how to turn it into code.
Any modifications will help.
x = gets.chomp.to_fwhere empty argument lists are omitted and force-casting with things likeFloat()is strongly discouraged.(float) xwhere that convertsxto afloat. Naturally some people think that the equivalent in Ruby would beFloat(x), since as far as syntax goes thats' the closest, but it's actuallyx.to_fdue to the principle of Duck Typing. In other words, any object that can handle a.to_fcall is an acceptable value, not necessarily one that plays nice withFloat.