12 Replies - 855 Views - Last Post: 30 May 2014 - 12:17 PM Rate Topic: -----

#1 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

Something is wrong with my loop logic

Posted 30 May 2014 - 11:23 AM

I don't know why this loop logic wont ever get to the if clauses that set proper_piece_selection to true. This loop will never meets its conditional. Also if there are any refactoring tips I would appreciate them

  def user_questions
    puts "what is your name?"
    user_name = gets.chomp
    puts "Would you like to be first (write X) or second (write O)?"
    user_turn_choice = gets.chomp.upcase
    proper_piece_selection = false
    until proper_piece_selection == true do
      if user_turn_choice != 'X' || 'O'
        puts "please write an 'X' if you would like to go first or an 'O' if you would like to go second"
        user_turn_choice = gets.chomp.upcase
      elsif user_turn_choice  == 'X'
        puts "Excellent. You have chosen to go first"
        proper_piece_selection = true
      else
        puts "You have chosen to go second. Bold move."
        proper_piece_selection = true
      end
    end
  end



Is This A Good Question/Topic? 0
  • +

Replies To: Something is wrong with my loop logic

#2 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 915
  • View blog
  • Posts: 3,195
  • Joined: 12-May 09

Re: Something is wrong with my loop logic

Posted 30 May 2014 - 11:32 AM

It would be helpful if you provided examples of what happens with different inputs (to both you and us). The ability to trace execution is an important skill in software development.

That said, I expect that line 8 is causing you problems.

If I rewrite it like this, do you see why?
if user_turn_choice != ('X' || 'O')



Try evaluating 'X' || 'O' , and it will return 'X' every time, because 'X' evaluates to true. This line doesn't ask if the user_turn_choice is one of X and O, but rather if user_turn_choice is equal to the result of 'X' || 'O'. In this case, that means the line ends up equivalent to if user_turn_choice != 'X'

The logic you're going for is:
if !['X', 'O'].include?(user_turn_choice)


This post has been edited by xclite: 30 May 2014 - 11:33 AM

Was This Post Helpful? 2
  • +
  • -

#3 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,358
  • Joined: 03-December 12

Re: Something is wrong with my loop logic

Posted 30 May 2014 - 11:34 AM

I would start with,

until proper_piece_selection == true



And loose the do.


Edited : better advice given.

This post has been edited by astonecipher: 30 May 2014 - 11:36 AM

Was This Post Helpful? 0
  • +
  • -

#4 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 915
  • View blog
  • Posts: 3,195
  • Joined: 12-May 09

Re: Something is wrong with my loop logic

Posted 30 May 2014 - 11:38 AM

There is solid style advice to be given here, as well =p

For example, my favorite thing to catch is:
if some_variable == true


or
if some_variable == false



Instead, just let the boolean evaluation work (we're being redundant):
if some_variable
if !some_variable


Was This Post Helpful? 1
  • +
  • -

#5 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

Re: Something is wrong with my loop logic

Posted 30 May 2014 - 11:55 AM

OK I made some of the corrections above with some additional changes as well. However, no matter what I type I still am trapped in the first if clause preventing the conditional from ever being met

def user_questions
  puts "what is your name?"
  user_name = gets.chomp.capitalize
  proper_piece_selection = false
  until proper_piece_selection
    if user_turn_choice != ('X' || 'O')
      puts "please write an 'X' if you would like to go first or an 'O' if you would like to go second"
      user_turn_choice = gets.chomp.upcase
    elsif user_turn_choice  == 'X'
      puts "Excellent #{user_name}. You have chosen to go first"
      proper_piece_selection = true
    else
      puts "#{user_name} you have chosen to go second. Bold move."
      proper_piece_selection = true
    end
  end
end



never mind still trying to understand the OR logic principal you illustrated above ignore my last response.
Was This Post Helpful? 0
  • +
  • -

#6 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 915
  • View blog
  • Posts: 3,195
  • Joined: 12-May 09

Re: Something is wrong with my loop logic

Posted 30 May 2014 - 11:56 AM

I think you missed what I was saying. Read my post again - I was putting the parens there to show what your line actually does. You can't do a check for group membership that way - you're still running the same check.
Was This Post Helpful? 1
  • +
  • -

#7 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2151
  • View blog
  • Posts: 3,307
  • Joined: 21-June 11

Re: Something is wrong with my loop logic

Posted 30 May 2014 - 11:58 AM

View Postxclite, on 30 May 2014 - 08:32 PM, said:

If I rewrite it like this, do you see why?
if user_turn_choice != ('X' || 'O')



This doesn't make much difference with respect to your point, but x != y || z is parsed as (x != y) || z, not x != (y || z).
Was This Post Helpful? 2
  • +
  • -

#8 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 915
  • View blog
  • Posts: 3,195
  • Joined: 12-May 09

Re: Something is wrong with my loop logic

Posted 30 May 2014 - 12:01 PM

Ha. I thought about checking the operator precedence but remained lazy. Good catch.
Was This Post Helpful? 0
  • +
  • -

#9 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

Re: Something is wrong with my loop logic

Posted 30 May 2014 - 12:02 PM

I understand the Or statement now. Thanks for the explanation. I made some changes to the code and am still not ever meeting the conditional no matter what I type. I'm stuck in the first if clause for eternity.

def user_questions
  puts "what is your name?"
  user_name = gets.chomp.capitalize
  proper_piece_selection = false
  until proper_piece_selection
    if !['X', 'O'].include?(user_turn_choice)
      puts "please write an 'X' if you would like to go first or an 'O' if you would like to go second"
      user_turn_choice = gets.chomp.upcase
    elsif user_turn_choice  == 'X'
      puts "Excellent #{user_name}. You have chosen to go first"
      proper_piece_selection = true
    else
      puts "#{user_name} you have chosen to go second. Bold move."
      proper_piece_selection = true
    end
  end
end


Was This Post Helpful? 0
  • +
  • -

#10 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 915
  • View blog
  • Posts: 3,195
  • Joined: 12-May 09

Re: Something is wrong with my loop logic

Posted 30 May 2014 - 12:05 PM

Can you provide sample input and output, as I mentioned?
Was This Post Helpful? 0
  • +
  • -

#11 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

Re: Something is wrong with my loop logic

Posted 30 May 2014 - 12:07 PM

If I type any letter as an input the ouput is "please write an 'X' if you would like to go first or an 'O' if you would like to go second"
Was This Post Helpful? 0
  • +
  • -

#12 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2151
  • View blog
  • Posts: 3,307
  • Joined: 21-June 11

Re: Something is wrong with my loop logic

Posted 30 May 2014 - 12:08 PM

In your original code you set user_turn_choice before the loop, but in your current code you don't. I'm surprised you don't get an exception now. Do you have a method named user_turn_choice defined?
Was This Post Helpful? 2
  • +
  • -

#13 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

Re: Something is wrong with my loop logic

Posted 30 May 2014 - 12:17 PM

no sir.

CORRECTION ; i had it as an attr_reader
I'm getting rid of that now

And yes when I get rid of that attr-reader - I do get an exception

OK so this version works! Let me know if any of you think this could use some refactoring. Thanks for the help.

def user_questions
  puts "what is your name?"
  user_name = gets.chomp.capitalize
  proper_piece_selection = false
  user_turn_choice = nil
  until proper_piece_selection
    if !['X', 'O'].include?(user_turn_choice)
      puts "please write an 'X' if you would like to go first or an 'O' if you would like to go second"
      user_turn_choice = gets.chomp.upcase
    elsif user_turn_choice  == 'X'
      puts "Excellent #{user_name}. You have chosen to go first"
      proper_piece_selection = true
    else
      puts "#{user_name} you have chosen to go second. Bold move."
      proper_piece_selection = true
    end
  end
end


This post has been edited by heaphyg: 30 May 2014 - 12:11 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1