diff --git a/guess.py b/guess.py old mode 100644 new mode 100755 index f68f125..8fb3b11 --- a/guess.py +++ b/guess.py @@ -1,29 +1,30 @@ +#!/usr/bin/python3 + import random number = random.randint(1, 20) guesses = 0 -print 'Hello! What is your name?' -name = raw_input() +print('Hello! What is your name?') +name = input() -print "Hi, {}. I'm thinking of a number from 1 and 20.".format(name) +print(f"Hi, {name}. I'm thinking of a number from 1 and 20.") while guesses < 6: - - print 'What is your guess. You have {} more guesses.'.format(6-guesses) - guess = raw_input() + print(f'What is your guess? You have {6 - guesses} more guesses.') + guess = input() guess = int(guess) guesses = guesses + 1 if guess < number: - print 'Too low.' + print('Too low.') elif guess > number: - print 'Too high.' + print('Too high.') elif guess == number: - print 'Good job, {}! You guessed my number in {} guesses!'.format(name,guesses) + print(f'Good job, {name}! You guessed my number in {guesses} guesses!') break if guess != number: - print 'Nope. The number I was thinking of was {}.'.format(number) + print(f'Nope. The number I was thinking of was {number}.') diff --git a/guess.rb b/guess.rb old mode 100644 new mode 100755 index 18a7c52..f15c2dc --- a/guess.rb +++ b/guess.rb @@ -1,30 +1,22 @@ +#!/usr/bin/env ruby + number = rand(1..20) -guesses = 0 puts 'Hello! What is your name?' -name = gets.chomp.to_s - -puts "Hi, #{name}. I'm thinking of a number between 1 and 20." +name = gets&.chomp -while guesses < 6 +puts "Hi, #{name}. I'm thinking of a number between 1 and 20." - puts "What is your guess? You have #{6-guesses} more guesses." - guess = gets.chomp.to_i - guesses += 1 +1.upto 6 do |guesses| + puts "What is your guess? You have #{7 - guesses} more guesses." + guess = gets&.chomp.to_i - unless guess == number - message = if guess > number - "Too high" - else - "Too low" - end - puts message - else + if guess == number puts "Good job, #{name}! You guessed my number in #{guesses} guesses." exit + else + puts(guess > number ? 'Too high' : 'Too low') end - end puts "Nope. The number I was thinking of was #{number}." - diff --git a/readme.md b/readme.md index b661e38..f281cd9 100644 --- a/readme.md +++ b/readme.md @@ -9,13 +9,13 @@ What do I mean by dynamic? Well, with a dynamically typed language you can do th ```sh >>> variable = 1 >>> type(variable) - + >>> variable = "Foo" >>> type(variable) - + >>> variable = ["bar",10] >>> type(variable) - + ``` Essentially, you can change the datatype (from an integer to a string to a list, in the above example) at any point in a program. In a statically typed language, this would result in an error when compiled. @@ -98,8 +98,7 @@ Let's compare some code. The following snippets of code are for solving the Fibo ```ruby def fib(n) - return n if n < 2 - fib(n-1) + fib(n-2) + n < 2 ? n : fib(n-1) + fib(n-2) end alias :fibonacci :fib ``` @@ -116,7 +115,7 @@ def fib(n): Although you can write this code in many ways, both of these methods are *true* to the language. -> The Ruby code demonstrates the practice of having multiple names for the same function (`fib` and `fibonacci` are the same). +> The Ruby code demonstrates the practices of using ternary operators and of having multiple names for the same function (`fib` and `fibonacci` are the same). In other words, the Ruby example is very Ruby-ish while the Python example is very Pythonic. Can you read the Ruby code? It may be more elegant but it's a bit harder to read. Meanwhile, it's easy to follow the Python code, right? You of course can write code anyway you want. It's advisable to write Ruby code, when beginning, in a more Pythonic way - which simply means making it more readable: @@ -130,7 +129,7 @@ def fib(n) end ``` -Keep in mind that in many cases with Python there are still a number of ways to do one thing. Take copying a list for example. There's a least four different ways: +Keep in mind that in many cases with Python there are still a number of ways to do one thing. Take copying a list for example. There are at least four different ways: ```python >>> my_list = [1,2,3] @@ -153,7 +152,7 @@ The difference is that there is one right way of doing this given the situation. ### More differences -As you can imagine, there are many more differences that just the syntax and philosophies of the two languages. Let's quickly look at some examples. +As you can imagine, there are many more differences than just the syntax and philosophies of the two languages. Let's quickly look at some examples. #### Learning Curve @@ -179,20 +178,20 @@ In the first example (Python), we are importing the `Twitter()` class from the ` #### Programming Paradigms -Again, you can use the same paradigms in both languages (procedural, functional, object oriented ...). When it comes to object oriented programming, Ruby used to have the upper hand, as it was built specifically for object orientation. That said, Python has moved more towards being a true object orientated language over the last few years. However, Ruby has one advantage, Ruby can add methods to existing classes, Python by default can't do this (although it's possible with the use of external libraries). +Again, you can use the same paradigms in both languages (procedural, functional, object oriented ...). When it comes to object oriented programming, Ruby used to have the upper hand, as it was built specifically for object orientation. That said, Python has moved more towards being a true object orientated language over the last few years. However, Ruby has one advantage: it can add methods to existing classes, while Python by default can't do this (although it's possible with the use of external libraries). #### Performance -Performance is a toss up as well. In some cases Python performs better, while in other cases, Ruby outperforms Python. It all depends on the task at hand. +Performance is a toss up as well. In some cases Python performs better, while in others Ruby outperforms Python. It all depends on the task at hand. #### Usage -Ruby has a bigger web presence with Rails than Python does with Django, so if you're looking to move into web development, Ruby may be the way to go. Python is a great general-purpose language and has more momentum going for it for areas outside outside of the web, such as sys admin/DevOps, statistics, and scientific computation. +Ruby has a bigger web presence with Rails than Python does with Django, so if you're looking to move into web development, Ruby may be the way to go. Python is a great general-purpose language and has more momentum going for it for areas outside of the web, such as sys admin/DevOps, statistics, and scientific computation. That said, take a look at the two code snippets below - ```python -print "Hello, World!" +print("Hello, World!") ``` and @@ -205,10 +204,15 @@ End users do not care about the syntactical differences; they just want to see " #### Community -The Python community is active, vibrant, and truly helpful. Although, you can say the same about the Ruby community, the community itself is very much tied into Rails. If Rails is your thing, then you are in luck. +The Python community is active, vibrant, and truly helpful. Although you can say the same about the Ruby community, the community itself is very much tied into Rails. If Rails is your thing, then you are in luck. #### Popularity/Jobs +![popularity](https://static1.squarespace.com/static/51361f2fe4b0f24e710af7ae/t/56b1187d4c2f85efc5598bb1/1454446752995/?format=750w) + +Source: http://blog.codeeval.com/codeevalblog/2016/2/2/most-popular-coding-languages-of-2016 + + ![popularity](https://raw.github.com/mjhea0/python-ruby/master/images/codeeval2015.jpg) For the fourth year in a row, Python is the most popular language. Also, notice how Ruby decreased in popularity: @@ -243,65 +247,55 @@ Guessing game ... ```python import random -import os + number = random.randint(1, 20) guesses = 0 -print 'Hello! What is your name?' -name = raw_input() +print('Hello! What is your name?') +name = input() -print "Hi, {}. I'm thinking of a number from 1 and 20.".format(name) +print(f"Hi, {name}. I'm thinking of a number from 1 and 20.") while guesses < 6: - - print 'What is your guess. You have {} more guesses.'.format(6-guesses) - guess = raw_input() + print(f'What is your guess? You have {6 - guesses} more guesses.') + guess = input() guess = int(guess) guesses = guesses + 1 if guess < number: - print 'Too low.' + print('Too low.') elif guess > number: - print 'Too high.' + print('Too high.') elif guess == number: - print 'Good job, {}! You guessed my number in {} guesses!'.format(name,guesses) + print(f'Good job, {name}! You guessed my number in {guesses} guesses!') break if guess != number: - print 'Nope. The number I was thinking of was {}.'.format(number) + print(f'Nope. The number I was thinking of was {number}.') ``` #### Ruby ```ruby number = rand(1..20) -guesses = 0 puts 'Hello! What is your name?' -name = gets.chomp.to_s +name = gets&.chomp puts "Hi, #{name}. I'm thinking of a number between 1 and 20." -while guesses < 6 - - puts "What is your guess? You have #{6-guesses} more guesses." - guess = gets.chomp.to_i - guesses += 1 +1.upto 6 do |guesses| + puts "What is your guess? You have #{7 - guesses} more guesses." + guess = gets&.chomp.to_i - unless guess == number - message = if guess > number - "Too high" - else - "Too low" - end - puts message - else + if guess == number puts "Good job, #{name}! You guessed my number in #{guesses} guesses." exit + else + puts(guess > number ? 'Too high' : 'Too low') end - end puts "Nope. The number I was thinking of was #{number}." @@ -319,10 +313,13 @@ Cheers! ### Resources +1. [Python vs Ruby](http://c2.com/cgi/wiki?PythonVsRuby) 1. [Why I push for Python](http://lorenabarba.com/blog/why-i-push-for-python/) -2. [4 Reasons Why You Should Learn Ruby As Your First Programming Language](http://www.skilledup.com/learn/programming/4-reasons-learn-ruby-first-programming-language/) -3. [How do Python and Ruby compare?](http://www.quora.com/How-do-Python-and-Ruby-compare/answer/Fabio-Akita?share=1) -4. [Why Python is a Great First Language](http://blog.trinket.io/why-python/) -5. [Python vs Ruby](http://www.scriptrock.com/articles/python-vs-ruby) -6. [Ruby on Rails vs Python and Django: Which Should a Beginner Learn?](https://www.coursereport.com/resources/ruby-on-rails-vs-python-and-django-which-should-a-beginner-learn) -7. [Python Environment Management for Rubyists – a Guide](http://spin.atomicobject.com/2015/01/02/python-environment-mgmt/) \ No newline at end of file +1. [4 Reasons Why You Should Learn Ruby As Your First Programming Language](http://www.skilledup.com/learn/programming/4-reasons-learn-ruby-first-programming-language/) +1. [How do Python and Ruby compare?](http://www.quora.com/How-do-Python-and-Ruby-compare/answer/Fabio-Akita?share=1) +1. [Why Python is a Great First Language](http://blog.trinket.io/why-python/) +1. [Python vs Ruby](http://www.scriptrock.com/articles/python-vs-ruby) +1. [Ruby on Rails vs Python and Django: Which Should a Beginner Learn?](https://www.coursereport.com/resources/ruby-on-rails-vs-python-and-django-which-should-a-beginner-learn) +1. [Python Environment Management for Rubyists – a Guide](http://spin.atomicobject.com/2015/01/02/python-environment-mgmt/) +1. [Ruby vs Python](https://www.coursereport.com/blog/ruby-vs-python-choosing-your-first-programming-language) +1. [Ruby vs Python, the Definitive FAQ](https://hackernoon.com/ruby-vs-python-the-definitive-faq-5cb0046292be)