Thursday, October 3, 2013

Python Lab

Python lab:


Python from the little I have played with python it shares some similarities with java. I do appreciate the fact that it forces you indent your code and make it closer to be universal but I do miss starting and ending brackets. I also miss ending code statements with ; This means that I cannot put another line of code in the same line. Variables are also handled differently as  you do not need to setup the type for them. This could maybe become a little bit confusing if not being careful because the variable could switch from int to string for example well if python permits that.


In general it is easy to start coding with python if there is a previous java/c experience. For the part Google is kind enough to help.



17 import random
18 import os
19 import sys
20
21 #Intro and tutorial
22 os.system('clear')
23 print ("#####################")
24 print ("#Rules of the game: #")
25 print ("#####################\n")
26 print ("Guess the right number from 1 to 100")
27 print ("The maximum number of tries is 15, good luck !!!\n")
28
29 try:
30      raw_input("press enter to continue...")
31 # Handle interrupts like ctrl+c
32 except KeyboardInterrupt:
33      print ("\nUntil next time quitter")
34      sys.exit()
35
36 # Initialize vars
37 guess = 0
38 numTries = 0
39 secret=random.randint(1,100)
40
41 # Loop until answer is found or number of tries gets to 15
42 while (secret != guess and numTries != 15):
43      # Catch exceptions
44      try:
45              guess = int(raw_input("\nEnter a guess: "))
46              if guess < secret:
47                    numTries = numTries + 1
48                    print "Too low! , Try #:", numTries
49              elif guess > secret:
50                    numTries = numTries + 1
51                    print "Too high! , Try #:", numTries
52              else:
53                    numTries = numTries + 1
54                    print "Correct!, Total number of tries:", numTries , "\n"
55              if numTries == 15:
56                        print "\nMaximum number of tries reached, Good luck next time !!!\n"
57
58      # Handle characters other than int
59      except ValueError:
60              print ("Please enter integers only")
61
62      # Handle interrupts like ctrl+c
63      except KeyboardInterrupt:
64              print ("\nUntil next time quitter")
65              sys.exit()



Here is my code. I did go a little bit further while playing with python. It is was really enjoyable. Of course improvements could be made … perhaps … but that is beyond the scope of this lab.



I went ahead and did everything in one commit. It was short enough to keep track of everything going in the code and find the errors instantly.



rhernandez2@matrix:~/play/guess> git commit -a -m "Initial and maybe last commit"
[master 430ea2a] Initial and maybe last commit
Committer: Ronald Hernandez <rhernandez2@matrix.senecac.on.ca>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:


git config --global user.name "Your Name"
git config --global user.email you@example.com


After doing this, you may fix the identity used for this commit with:


git commit --amend --reset-author


1 file changed, 52 insertions(+), 8 deletions(-)