どちらもInterpreter Languageだが、ひとつを選べと言われたらどちらを覚えたほうがよいのだろう。
日本ではRubyがかなり人気があるようだが、WorldwideのUser BaseではPythonのほうが多い。 多いと言ってもPHPとかに比べるとかなり少ない。
Ruby
# Old "coke" program from 80's Basic programming book in Ruby
answer = Random.rand(1..100)
count,guess = 0,0
puts 'I am thinking about number between 1 and 100. Can you guess?'
while (answer != guess)
guess = gets.to_i
count += 1
if guess > answer
puts "#{guess} is too high"
elsif guess < answer
puts "#{guess} is too low"
else
puts "you got it right in #{count} try!"
end
end
Python
"""Old "coke" program from 80's Basic programming book, in Python"""
import random
answer = random.randrange(1,100)
print ("I am thinking a number between 1 and 100. Hope you can guess")
guess, count = 0,0
while (answer != guess ):
guess = input ("what number am I thinking? ")
guess = int(guess)
count += 1
if answer < guess:
print ( guess, " is too high, try again")
elif answer > guess:
print ( guess," is too low. Try again")
else:
print ("Bingo! you guessed it right in ",count," tries!")
とりあえず調べて見えてきた部分で大昔、Basicで初期に書いた練習用プログラムを移植してみたが、ごらんのとおり、これくらいのサイズのプログラムなら、どちらで書いてもほとんど差がない。言語のパワフルなところを使ってみないとその差がわからない、という感じだが、 ルーズに行くならPythonかな?インデントでブロックを明示している、つまり単なる書式ではなく、言語の一部になっているのが、なんとも割り切っているところ。 これで{}も"End"も必要ない。 個人的には好きである。 どちらの言語も読み解きやすいのはまちがいなく、これをC++ で書こうとすると、
#include <iostream>
#include <cstdlib> //need this to use rand()
#include <ctime> //need this to use time()
using namespace std;
int main(){
int answer, guess=0, count=0;
srand(time(NULL));
answer = rand()%100 + 1;
cout << "I am thinking about the number between 1 and 100. Can you guess?";
while (guess != answer) {
++count;
cin >> guess;
if (guess > answer)
cout << guess << "is too high";
else if (guess < answer)
cout << guess << "is too low";
else
cout <<"You have it right in " << count << " times!";
cout << endl;
}
return 0;
}
これよりは楽ですよ。