Feeds:
Posts
Comments

Archive for the ‘Shell Scripting (SPR720)’ Category

* Display the longest and shortest usernames on the system (usernames are in the first field in /etc/passwd).

script22

* Loop through the files in your home directory, and for each readable file, ask whether the file should be printed, mailed to you, or ignored (P/M/I) and then take the appropriate action.

script1

*Count the number of files in the user’s home directory which are not readable.

six2

Advertisements

Read Full Post »

     Finally Python is coming… hopefully in few time we will be a Python-geeks… so with that in mind, hands-on!.
This was the task:
…………………………………………………………………………………………………………………………………………………………………………………………………….

1. Save this script, set the permissions correctly, and run it.

#!/usr/bin/python

import random
secret=random.randrange(1,100)

guess=int(raw_input(“Enter a guess: “))
if guess<secret:

……….print “Too low!”
elif guess>secret:
……….print “
Too high!
else:
……….print “Correct!”

2. Modify the script so that the user is given playing instructions before the game starts.
3. Make the script loop to prompt for input until the the correct number is guessed.
4. Displays the number of guesses the user took to guess the secret (score).
5. Modify the script so that it does not stop when invalid data is entered (such as a word). Suggestion: use try/except.
6. Accept only input value from 1 to 100.
7. Write a blog entry giving your impressions of Python.

…………………………………………………………………………………………………………………………………………………………………………………………………….

So, after add the required stuff I got something like this:
…………………………………………………………………………………………………………………………………………………………………………………………………….
#!/usr/bin/python

print “This game is about a number that the system will generate for you to guess, “
print “so, enter a number in between 1 and 100 “
print “you will have until 10 chances… ready? GO!”

import random
secret=random.randrange(1,100)
PlayerScore=10
Counter=0

# Verify that the Input from user is correct accord to the game rules, otherwise “raise” a error message…
def ValidateInput(x, min=1, max=100):
……….try:
……….……….if not min <= x <= max:
……….……….……….raise ValueError(‘That number is not in between 1 and 100…’)
……….……….else:
……….……….……….return 1
……….except ValueError:
……….……….print “That number is not in between 1 and 100…”
……….……….return 0

while True:
……….try:
……….……….Counter+= 1
……….……….guess=int(raw_input(“Enter a guess: “))
……….……….ValAnswer=ValidateInput(guess)
……….……….if ValAnswer==0:
……….……….……….continue
……….……….else:
……….……….……….if guesssecret:
……….……….……….……….print “Nop… lower than that…”
……….……….……….……….PlayerScore-=1
……….……….else:
……….……….……….……….print “You have guess!!!!”
……….……….……….……….break
……….……….if Counter > 9:
……….……….……….……….print “Sorry but this is taking to much time!… GAME OVER!”
……….……….……….……….break
……….except ValueError:
……….……….print “This program just accept numbers…”

print
print
“Stats: “
if Counter <= 3:
……….print “You tried to guess “,Counter , “times. That was really good!!! CONGRATULATIONS!”
else:
……….print “You tried to guess “,Counter , “times.”
print “Your score: “,PlayerScore
…………………………………………………………………………………………………………………………………………………………………………………………………….
I’m marking the indentation in each line because in my opinion is one of the basic things that we should care.

I want to share some things that I learned trough this experience:

     In Python Indentation is very important, there is no way to close a loop with some instruction, but the correct indentation, it was a pain in.. the brain! so since I’m not get used to its syntax was kind of difficult, Python is strict on this; for example, I was forgetting add “:” after “try” or “while True”… which by the way can not be “true” should be “True”… (I guess the compiler was having fun letting me know that I had gotten some mistakes…)

     But still I want to get into this, the power of Python programming is amazing, because is Cross-platform… very important feature!

Read Full Post »