From: sto.mar@... Date: 2013-04-12T02:16:57+09:00 Subject: Re: confusion with gets, if Am 11.04.2013 17:16, schrieb Daniel ferrando: > hi I'm new to ruby and im trying to write some simple codes but i cant > get the 'if's to work. below what i mean. how can i fix the ruby code > for the ifs to work? > > no matter what i write it always returns "invalid input" > ##################Code######################### > while counter < 1 do > print ("put 10, 20 or 30 ") here, the extra space is not a problem, but you should use print("put 10, 20 or 30 ") or simply: print "put 10, 20 or 30 " > input = gets() > if input == '10' The source of the problem can be seen here: 2.0.0-p0 :001 > input = gets 10 => "10\n" the returned string contains an new line character! possible solutions: 1. input = gets.chomp # the parentheses are not needed if input == '10' ... 2. input = gets if input == "10\n" # mind the double quotes! ... And: it's always good practice to try code using IRB. > person1 += 1 > counter += 1 > elsif input == 20 > person2 += 1 > counter += 1 > elsif input == 30 > person3 += 1 > counter += 1 > else > puts("Invalid input") > end > > > end > ############################################## As mentioned in a different post you would need strings for 20 and 30, too. > if i were to write it in python i would do it somewhat like this: > > ##################Code######################### > while counter < 5 : > > vector.append(input('put 10, 20 or 30 ')) > > if vector [counter] == '10' : > p1 += 1 > counter += 1 > elif vector [counter] == '20': > p2 += 1 > counter +=1 > elif vector [counter] == '30': > p3 += 1 > counter += 1 > else: > print ("invalid input") > > ############################################## I'm not exactly sure what you are trying to do, but I don't care much for the while loop and all the counters. > last question whats the difference between ruby and ruby on rails? Rails is a web framework written in Ruby, maybe comparable to Django in the Python world. --