Thursday, August 9, 2012

Python to Ruby - Guessing game

I taught myself python a year ago.

I did not get too far with it. I learned the basics, went on to django, found myself facing a wall, got frustrated and decided to do nothing about it.

Reading about ruby bores me. I'd rather do something, and learn as I go. I learn better this way.

So, to learn ruby, I am going to get exercises from the book, "Invent your own computer games with python". I will convert the python games from that book into ruby, then post them in this blog.

Here's the second python game code from that book that I converted to ruby.

PYTHON
# This is a guess the number game.

import random

guessesTaken = 0
print 'Hello! What is your name?'
myName = raw_input()
number = random.randint(1, 20)
print 'Well, ' + myName + ', I am thinking of a number between 1 and 20.'

while guessesTaken < 6:

print 'Take a guess.'
guess = raw_input()
guess = int(guess)
guessesTaken = guessesTaken + 1

if guess < number:
print 'Your guess is too low.'

if guess > number:
print 'Your guess is too high.'

if guess == number:
break

if guess == number:
guessesTaken = str(guessesTaken)
print 'Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!'

if guess != number:
number = str(number)
print 'Nope. The number I was thinking of was ' + number



Here is how I translated it into RUBY...
guessTaken = 0
guessStr = guessTaken.to_s

puts "Hello! What is your name?"
player = gets.chomp

number = rand(20)
num1 = number.to_s
puts "Well #{player}, I am thinking of a number between 1 and 20."

# can make a function here?
while guessTaken < 6
  puts 'Take a guess.'
  guess = gets.to_i
  guessTaken = guessTaken + 1

  if guess < number
    puts "Your guess is too low."
  elsif guess > number
    puts "Your guess is too high."
  else
    break
  end
end

if guess == number
  puts "Good job #{player}! You guessed my number in #{guessStr} guesses!"
end

if guess != number
  puts "Nope. The number I was thinking of was #{num1}."
end

Looking at the two codes above, one might say that python code looks cleaner and only takes less lines to do the job. I agree. Python code takes up less space, as blocks do not have to have "end" to them.

In my experience though, I find it easier to understand ruby. There's a lot of good python tutorials out there, and I must say that some of these are better written than the ones I find with ruby.

But ruby seems easier for me to understand--at my level. I don't know if this is going to be so the more I go deeper into ruby. But looking at the syntax, and how I it is easier for me to understand the language just by looking at it, I am going to stick with ruby.

EDIT(Nov. 2, 2012):
I made changes to this game's code. Here it is...
puts "#{myName}, I am thinking of a number between 1 and 6. Guess what it is."
myNum = rand(1..6)
tries = 0

while tries < 6
    puts 'Take a guess...'
    guess = gets.chomp
    a = guess.to_i
    tries = tries + 1

    if a < myNum
        puts "#{guess} is too low. Try again."
    elsif a > myNum
        puts "#{guess} is too high. Try again."
    else a == myNum
        puts "Great! You guessed my number in #{tries} attempts."
        break
    end
end

No comments:

Post a Comment