컨테이너와 반복문의 만남
Python
1 2 3 4 5 | members = [ 'egoing' , 'leezche' , 'graphittie' ] i = 0 while i < len (members): print (members[i]) i = i + 1 |
Ruby
1 2 3 4 5 6 | members = [ 'egoing' , 'leezche' , 'graphittie' ] i = 0 while i < members.length do puts(members[i]) i = i + 1 end |
실행결과
1 2 3 | egoing leezche graphittie |
달콤한 for문의 등장
Python
1 2 3 | members = [ 'egoing' , 'leezche' , 'graphittie' ] for member in members: print (member) |
Ruby
1 2 3 4 | members = [ 'egoing' , 'leezche' , 'graphittie' ] for member in members do puts(member) end |
실행결과
1 2 3 | egoing leezche graphitti |
for문의 활용
Python
1 2 | for item in range ( 5 , 11 ): print (item) |
Ruby
1 2 3 | for item in ( 5 .. 10 ) do puts(item) end |
실행결과
1 2 3 4 5 6 | 5 6 7 8 9 10 |
로그인 애플리케이션에 투입
Python
1 2 3 4 5 6 7 8 | input_id = input ( "아이디를 입력해주세요.\n" ) members = [ 'egoing' , 'k8805' , 'leezche' ] for member in members: if member = = input_id: print ( 'Hello!, ' + member) import sys sys.exit() print ( 'Who are you?' ) |
Ruby
1 2 3 4 5 6 7 8 9 10 | puts( "아이디를 입력해주세요" ) input_id = gets.chomp() members = [ 'egoing' , 'k8805' , 'leezche' ] for member in members do if member == input_id puts( 'Hello!, ' +member) exit end end puts( 'Who are you?' ) |
실행결과
입력 값에 따라서 달라요~