[Tutor] (no subject)

Aswin Shan as.winshan at hotmail.com
Mon Dec 12 06:49:03 CET 2011


Hi!
I need help in creating a GUI for my python code to program a Hangman. I have programmed a working code of Hangman, but I need to make it to a proper program with GUI. please help. 
The code is given below:

import random;
import time;

correct_guesses = ['-', ' ']
guessed_letters = []


def Input_Error(input):
        if input.isdigit() == True:
            print "Input Error. \nIt's Hangman. Your secret word only includes letters. "
            while input.isdigit() == False:
                input = raw_input('Guess a letter: ')
        if len(input)>1:
            print 'Only enter one guess at a time'
                
def Get_Random_Word():
    global word
    word_list = []
    for line in open('dictionary.txt'):
        word_list=line.split()
    wordno=random.randint(0, len(word_list) -1)
    word= word_list[wordno]
    print word
    return word

def displayBoard():
    display = []
    i = 0
    while i < len(word):
        if word[i] in correct_guesses:
            display.append(word[i])
        if word[i] not in correct_guesses:
            display.append('_ ')
        i +=1
    for w in display:
        print w,

def play():
    global player_guess
    global guess
    player_guess = (raw_input('\nGuess a letter: ')).lower()
    Input_Error(player_guess)
    guess = 0
    while guess < 9:
        print guess
        if player_guess.lower() in guessed_letters:
            print "You have guessed this letter already"
        elif player_guess in word:
            guessed_letters.append(player_guess)
            correct_guesses.append(player_guess)
        elif player_guess not in word and player_guess.isdigit()== False:
            guessed_letters.append(player_guess)
            print 'wrong'
            guess += 1
        if len(correct_guesses)-2 == len(word):
            print word
            print 'Congratulation, you guessed the word correctly in', guess, 'guesses.'
            break
        if guess == 8:
            break
        displayBoard()
        player_guess = (raw_input('\nGuess another letter: ')).lower()
        Input_Error(player_guess)

def Welcome():
        print """        | |   | |  /\  |  ___ \ / _____)  ___ \   /\  |  ___ \ 
        | |__ | | /  \ | |   | | /  ___| | _ | | /  \ | |   | |
        |  __)| |/ /\ \| |   | | | (___) || || |/ /\ \| |   | |
        | |   | | |__| | |   | | \____/| || || | |__| | |   | |
        |_|   |_|______|_|   |_|\_____/|_||_||_|______|_|   |_|
                                                       
                      Welcome to Hangman v1.0

Rules:
1. You will have 8 chances to guess the letters correctly.
2. For each wrong guess one chance will be decremented.
3. If you guess the same word again, the chances will not be decremented.

Good luck."""
        print "Generating your secret word..."
        time.sleep(3)
        

        
Welcome()
Get_Random_Word()
displayBoard()    
play()
                                

I also find trouble with the some hangman pic which I have provided below. I shows a EOL Error. 
def Answer_Feedback(w_count):
    if w_count==1:
        print """
  +---+
  |   |
      |
      |
      |
      |
=========
Wrong!  """
        
    elif w_count == 2:
        print """
  +---+
  |   |
  O   |
      |
      |
      |
=========
Wrong!  """
    elif w_count == 3:
        print """
  +---+
  |   |
  O   |
  |   |
      |
      |
=========
Wrong!  """
    elif w_count == 4:
        print """
  +---+
  |   |
  O   |
/|   |
      |
      |
=========
Wrong!  """
    elif w_count == 5:
        print """
  +---+
  |   |
  O   |
/|   |
      |
      |
=========
Wrong!""""
    elif w_count == 6:
        print """
  +---+     
  |   |    
  O   |   
/|\  |     
      |     
      |
=========         
Wrong!"""
    elif w_count == 7:
        print """
  +---+
  |   |
  O   |
/|\  |
/    |
      |
=========   
Wrong! """
    elif w_count == 8:
        print """
  _______
  |     |
  |     O
  |   ^-|-^
  |     |
  |     |
_|__  / \

You lost!!! """





Thank you thanks for the help.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111211/c1a02224/attachment-0001.html>


More information about the Tutor mailing list