Joc Python: Hangman

17 Mai 2016

#importing the time module
import time

#welcoming the user
name = raw_input(“What is your name? “)

print “Hello, ” + name, “Time to play hangman!”

print ” “

#wait for 1 second
time.sleep(1)

print “Start guessing…”
time.sleep(0.5)

#here we set the secret
word = “secret”

#creates an variable with an empty value
guesses = ”

#determine the number of turns
turns = 10

# Create a while loop

#check if the turns are more than zero
while turns > 0:

# make a counter that starts with zero
failed = 0

# for every character in secret_word
for char in word:

# see if the character is in the players guess
if char in guesses:

# print then out the character
print char,

else:

# if not found, print a dash
print “_”,

# and increase the failed counter with one
failed += 1

# if failed is equal to zero

# print You Won
if failed == 0:
print “You won”

# exit the script
break

print

# ask the user go guess a character
guess = raw_input(“guess a character:”)

# set the players guess to guesses
guesses += guess

# if the guess is not found in the secret word
if guess not in word:

# turns counter decreases with 1 (now 9)
turns -= 1

# print wrong
print “Wrong”

# how many turns are left
print “You have”, + turns, ‘more guesses’

# if the turns are equal to zero
if turns == 0:

# print “You Loose”
print “You Loose”

Anuncis

tr (translation) modify string in a text file

28 gener 2013
    • tr

(translation). A filter useful to replace all instances of characters in a text file or “squeeze” the whitespace.

Example:


cat some_file | tr '3' '5' > new_file

This will run the cat program on some file, the output of this command will be sent to the trcommand, tr will replace all the instances of 3 with 5, like a search and replace. You can also do other things such as:


cat some_file | tr '[A-Z]' '[a-z]' > new_file

This will run cat on some_file and convert any capital letters to lowercase letters (you could use this to change the case of file names too…).

 

source: http://linux.about.com/od/lts_guide/a/gdelts43_3.htm

Tee Command Usage Examples

28 gener 2013

Tee command is used to store and view (both at the same time) the output of any other command.

Tee command writes to the STDOUT, and to a file at a time as shown in the examples below.

Example 1: Write output to stdout, and also to a file

The following command displays output only on the screen (stdout).

$ ls

The following command writes the output only to the file and not to the screen.

$ ls > file

The following command (with the help of tee command) writes the output both to the screen (stdout) and to the file.

$ ls | tee file

Example 2: Write the output to two commands

You can also use tee command to store the output of a command to a file and redirect the same output as an input to another command.

The following command will take a backup of the crontab entries, and pass the crontab entries as an input to sed command which will do the substituion. After the substitution, it will be added as a new cron job.

$ crontab -l | tee crontab-backup.txt | sed 's/old/new/' | crontab –

Misc Tee Command Operations

By default tee command overwrites the file. You can instruct tee command to append to the file using the option –a as shown below.

$ ls | tee –a file

You can also write the output to multiple files as shown below.

$ ls | tee file1 file2 file3

Source: http://linux.101hacks.com/unix/tee-command-examples/

Esborrar arxiu més antics que una data

10 Agost 2009

Per esborrar arxiu més antics que una data determinada amb el find, fer:

find . -atime +3 -exec rm {} \;

Referència: http://www.comandos-linux.we.bs/comando-find.html

Restar dates en un script GNU

10 Agost 2009

Per poder restar dates en un script posar la instrucció:

date.exe –date=”9 days ago” +%d%m%Y

date.exe –date=”9 weeks ago” +%d%m%Y

date.exe –date=”9 months ago” +%d%m%Y

NOTA: — es refereix a dos guions seguits: “- -” no a un sol com aparenta.

Resta dies, setmanes o mesos en un script fet amb sh.exe de les unixtools que podeu baixar de: http://unxutils.sourceforge.net/

Extret de: http://www.unix.com/unix-dummies-questions-answers/7919-subtracting-two-months-date.html