0

So I'm learning ruby, and I have a script that I'm working on. It's supposed to ask your name and for a number, and determine if the number is even or odd. If its even, it will print a message saying so. If it's odd, it will ask for another number. However, if you put an odd number, then try to enter an even number, it will go through an endless loop of asking for another number. Any help is appreciated! Code:

    name = ask "What is your name?"
    num = ask name + ", " + "Enter a number"
    sumNum = num % 2
    while(sumNum != 0)
    num = ask "Pick another number" 
    end

    puts name + " picked an even number!"
Apollo503
  • 13
  • 6
  • I assume `ask` is a Rails method but you have no Rails tag. `sumNum` is not changed within the `while/end` loop, so if you enter the loop you will remain there forever. One Ruby convention is to use [snake case](https://en.wikipedia.org/wiki/Snake_case) for the names of variables and methods, so `sumNum` would normally be written `sum_num`. `name + ", " + "Enter a number"` can be simplified to `name + ", Enter a number"` or `#{name}, Enter a number"`. – Cary Swoveland Nov 17 '18 at 03:13

2 Answers2

0

Please read How to create a Minimal, Complete, and Verifiable example. Your current question does not provide enough data to know for certain what the all the issues are. For example, your ask method is not defined. It means I have to guess about what you're doing.

But I can tell you for certain that one issue is this code:

while(sumNum != 0)
  num = ask "Pick another number" 
end

Your loop condition is based on sumNum, but nowhere in your loop are you doing anything that would modify sumNum. A naive approach might be:

while(sumNum != 0)
  <do something that modifies the value of sumNum>
end

But since I'm missing the rest of your code I figured I would refactor your code into something that works:

puts 'What is your name?'
name = gets.chomp

# Don't use while/end: https://stackoverflow.com/a/10713963/3784008
loop do
  puts 'Enter a number'
  # gets returns a string that must be converted to an Integer
  num = gets.chomp.to_i
  # No need for % if all you care about is determining parity
  break if num.even?
end

puts name + ' picked an even number!'
anothermh
  • 9,815
  • 3
  • 33
  • 52
0

for checking odd or even ruby has the built-in method odd? this will return true if num is odd in our case we want to create while loop until the number we input is odd. So we can do something link this.

require 'highline/import'

name = ask "What is your name?"
ask_num = Proc.new {|msg| ask name + ", " + msg } #block code which can reused.
num = ask_num.call("Enter a number")
while num.to_i.odd? # loop works until num is odd
  num = ask_num.call("Pick another number")
end
puts name + " picked an even number!"

Proc objects are blocks of code which can be called based on different context. here we used it to show a different message while inputting number and retrying it. which helps to follow Dry policy were we are not rewriting the same set of code just reuse this block with different messages. Proc Reference

Vishal Taj PM
  • 1,339
  • 11
  • 23