#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0

    Problem with branch and loop


    Hi guys, I'm new at programming and while I was writing a program that generates a random password I had a problem. The thing is that the program asks you if you want downcase, upcase and numbers characters. So first questions are asked and then the program runs. I tried this:
    PHP Code:
    while numbers_reply != 'yes' or numbers_reply != 'no'
        
    print 'Please type "yes" or "no": '
        
    numbers_reply gets.chomp
        puts
    end 
    But it wouldn't run I don't exactly know why..., so I thought about doing this:
    PHP Code:
    'a'
    while != 'b'
        
    if numbers_reply == 'yes'
            
    'b'
        
    elsif numbers_reply == 'no'
            
    'b'
        
    else
            print 
    'Please type "yes" or "no": '
            
    numbers_reply gets.chomp
            puts
        end
    end 
    Could somebody tell me why the 1st version doesn't run and if you know a way of doing it shorter than the 2nd? Thank you!!
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    next time, please tell us exactly what the problem is. Don't just say that your program "doesn't run", because that doesn't tell us anything. We're not sitting in front of your computer, and we can't read your thoughts, so we have no idea what you see and what you expect.

    Your first script has a logical error: You keep asking the user as long as the input is different from "yes" or from "no". In other words, the program only stops if the input is "yes" and "no" at the same time, and that's obviously impossible. What you want is a logical and: You keep going as long as the input is neither "yes" nor "no".

    You should use an until loop to make the logic easier to understand. Negative conditions tend to be confusing.

    Code:
    numbers_reply = gets.chomp
    until numbers_reply == 'yes' or numbers_reply == 'no'
      print 'Please type "yes" or "no": '
      numbers_reply = gets.chomp
      puts
    end
    Note that Ruby code is supposed to be indented by exactly two spaces.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0
    Ok, thank you very much. I didn't know the method Until before, it will help me a lot. And next time I'll try to make my post more understandable

IMN logo majestic logo threadwatch logo seochat tools logo