Saturday, January 29, 2011

Guess The Number Simple Game in Python

I am learning python at this moment and created simple game. I saw some tutorial before and just coded this.

#!/usr/bin/env python3.1
#This is a guess the number game.
import random;

guessesToken = 0;

print("Hello! What is your name?");
myName = input();

number = random.randint(1, 20);

print("Well, Myfriend "+myName+" let's play a game");
print("Guess a number between 1 and 20");

while guessesToken < 6:
 guess = input();
 guess = int(guess);
 
 guessesToken = guessesToken + 1;
 
 if guess < number:
  print("Your guess is too low!");
 if guess > number:
  print("Your guess is too high!");
 if guess == number:
  break;
if guess == number:
 guessesToken = str(guessesToken);
 print("You guessed it within "+guessesToken+" times congratulations!");

if guess != number:
 print("You failed man!");

1 comment:

  1. #!/bin/bash
    ERROR=1
    while [ $ERROR -eq 1 ]
    do
    clear
    ERROR=0
    echo "L - List File Contents"
    echo "R - Rename File"
    echo "D - Delete File"
    echo "S - Statistics"
    echo "Q - Quit"
    echo -ne "\nYour Selection: "
    read SEL
    if [[ $SEL = "L" || $SEL = "l" ]]
    then
    clear
    cat $1
    echo -ne "\nPress any key to continue....."
    read -n1 $KEY
    ERROR=1
    elif [[ $SEL = "R" || $SEL = "r" ]]
    then
    clear
    echo -n "Enter new name for file: "
    read NAME
    mv $1 $NAME
    ERROR=0
    elif [[ $SEL = "D" || $SEL = "d" ]]
    then
    while [ $ERROR -eq 0 ]
    do
    clear
    echo -n "Are you sure you want to delete the file? (y/n): "
    read YN
    if [[ $YN = "Y" || $YN = "y" ]]
    then
    rm $1
    break
    elif [[ $YN = "N" || $YN = "n" ]]
    then
    ERROR=1
    else
    continue
    fi
    done
    elif [[ $SEL = "S" || $SEL = "s" ]]
    then
    clear
    ls -l $1
    echo -ne "\nPress any key to continue....."
    read -n1 $KEY
    ERROR=1
    elif [[ $SEL = "Q" || $SEL = "q" ]]
    then
    ERROR=0
    else
    ERROR=1
    fi
    done

    ReplyDelete