I have never done any programming in my life but I have decided to go into engineering and in doing so we have to take this intro to programming course and I am pretty clueless. I am starting to get the hang of how python works but to put my thoughts into the program to make it run is the confusing part for me.

The task is to implement a Hangman game and the Program Specifications are:

1) Output a brief description of the game of hangman and how to play
2) Ask the user to enter the word or phrase that will be guessed (have a friend enter the
phrase for you if you want to be surprised)
3) Output the appropriate number of dashes and spaces to represent the phrase (make sure
it’s clear how many letters are in each word and how many words there are)
4) Continuously read guesses of a letter from the user and fill in the corresponding blanks if
the letter is in the word, otherwise report that the user has made an incorrect guess.
5) Each turn you will display the phrase as dashes but with any already guessed letters filled
in, as well as which letters have been incorrectly guessed so far and how many guesses
the user has remaining.
6) Your program should allow the user to make a total of k=6 guesses.
7) You MUST use at least 3 string methods or operators in a useful manner (several
examples that I used are given in the notes below). If you wish to use lists in your project
that is fine, as long as you meet this requirement.

Recommended Answers

All 18 Replies

These are the tips my professor has given us,

Tips/Hints:
This project can all be done with strings, use help(str) in the python shell window to see all of the
string methods that may be useful. Some of the ones I used in the example program are below:
1) The string concatenation operator “+”. In order to keep track of the incorrect guesses, you
could initialize a blank string and use “+” to update the string with the incorrect guesses.
2) The membership operator “in” would be useful to check if a particular letter/digit has already
been guessed (correctly or incorrectly).
3) String slicing is useful to insert a letter in a string. For example if I have x = “hello” and I
wanted to put a ‘Z’ in the middle of the word, I could write:
x = x[0:3] + ’Z’ + x[3:]
or if I wanted to ‘Z’ to replace the ‘e’ I could write:
x = x[0:1] + ‘Z’ + x[2:]
remember string indexing using slices includes the start position but not the end position, so
x[0:2] is “he” but does not include the ‘l’ at index 2.
4) lower() can be used to change a string to lowercase – make sure you find the letter the user
enters whether it’s lower or uppercase in the guess word.
5) find() returns the index at which the first instance of a substring is found in a string, for
example if x=”hello”
x.find(‘e’) returns 1 and x.find(‘l’) returns 2.
6) remember if there is an apostrophe (‘) or a dash (-) in the original phrase you should output it
instead of a dash, the user doesn’t have to guess those.
7) Try the example program with many different inputs and make sure your program behaves
similarly!

I am willing to learn and put as much effort as needed I really want to understand. Any help would greatly be appreciated.

you'll need to plan out your program a bit, for example the crux is traversing a string and seeing if a guessed letter exists in it:

check_for_letter(string, letter):
for i in string:
if string[i-1] == letter
return True
else
return False

This function is just outlined roughly, but the logic for checking the string is there. Furthermore, you'll need to expand on this, you'll need a way to keep track of good and bad guesses (you may have a limit of 5 guesses or whatever..) and you'll also need a way to keep track of letters that have already been guessed.

I'm writing out what I need to do for the program but knowing that I am not well-versed in the python language I really don't know how to set it up so that python can compute it. Can anyone help me with that first I would appreciate it a lot. I am typing stuff into python and testing it out as of right now, if I get something to work I'll post my code and any suggestions or recommendations would greatly be appreciated!

Okay so this is what I have so far:

word='boring'
word_guess="_" *len(word)
letters_used=()

MAX_WRONG= len(word)
wrong=0

choice=raw_input('Guess a letter (lower case only):')

if len(choice)>1:
print 'Please enter only a letter please'

while (wrong < MAX_WRONG) and (letters used !=0):
print 'So far the word is:', word_guess
print 'Letter(s) you have used:', letters_used


I know I am missing a lot more other things and I would be really grateful if I can get some help anything would be appreciated! When I run it the loop keeps on going and I can't seem to get it to do what I want, again any help would greatly be appreciated.

letters_used and wrong are never incremented. You should be able to tell that from the print letters_used statement. Also, you define letters_used=() (a tuple) and not zero.

"6) Your program should allow the user to make a total of k=6 guesses."
Hmmmm, well at least doesn't have to keep track of right and wrong guesses.

First a little hint

print 'this is a description of my program'

then a little example on how to run through a string testing each letter

word = 'proofread'
guess_word = []
for x in range(len(word)):
  guess_word.append('_')
guesses = 0
k = 6
while(guesses < k):
  c = raw_input('input a letter')
  guesses = guesses + 1
  for x in range(len(word)):
    if (c == word[x]):
      guess_word[x] = c
      print guess_word
print 'You have used your 6 guesses'

If I didn't miss anything this and your own code should make it pretty easy to get the rest done.

"6) Your program should allow the user to make a total of k=6 guesses."
Hmmmm, well at least doesn't have to keep track of right and wrong guesses.

First a little hint

print 'this is a description of my program'

then a little example on how to run through a string testing each letter

word = 'proofread'
guess_word = []
for x in range(len(word)):
  guess_word.append('_')
guesses = 0
k = 6
while(guesses < k):
  c = raw_input('input a letter')
  guesses = guesses + 1
  for x in range(len(word)):
    if (c == word[x]):
      guess_word[x] = c
      print guess_word
print 'You have used your 6 guesses'

If I didn't miss anything this and your own code should make it pretty easy to get the rest done.

Thank You! that clearifies some of it up for me!

I also have to keep track of my right and wrong guesses so would that mean I have to set a variable to an empty string?

I believe I am in the same class as Her-o!
I'm a bit further along, but not much - perhaps someone can help me. Here's my issue:
Part of our program requires us to represent letters in the phrase to be guessed with "_", spaces with " ", dashes with "-", and apostrophes with "'".

Here's the code so far:

# Project 4
# Hangman Program
# KDW 2008

import string

print 'Welcome.  You have elected to play Hangman, a popular word and phrase game.  You will be prompted for a phrase to guess (have a friend enter one if you want to be surprised).  You will then guess letters until you have completed the phrase, or your missed chances run out.  You are allowed a total of six (6) missed guesses.  Good luck.'

word = raw_input('Enter a word to guess: ')
chances = 6
guesses = ''
incorrect = ''

while chances > 0:
    missed = 0
    for letter in word:
        if letter in guesses:
            print letter,
        else:
            print '_',
            missed += 1
        
    print
    print 'Incorrect guesses so far:',incorrect,
    print
    
    if missed == 0:
        print 'Winner!'
        break

    guess = raw_input('Guess a letter: ')
    guesses += guess
    
    if guess not in word:
        chances -= 1
        incorrect += guess
        print 'Sorry, try again.'
        print chances, 'chances left.'
        if chances == 0: print 'The answer is',word,'.'

How would I replace (space) in the "word" input with a literal " "? Same with the other special characters?

Thanks,
Kyle

" " is just a space with 2 quotes around it, if you want to put all 3 signs in your word there is two ways:

Since Python allows strings to be surrounded by either ' or " of your own choice you can do it the test1 way (just the opposite sign to enclose your string than what you want to include inside the string), or you can use escape sequences like test2

test = '" "'
test2 = "\" \""

Of course if you are just scanning through a string and wanna replace any space found with " " you need to do semething like:

for element in string:
  if (element == ' '):
    newstring += '" "'
    continue
  if (element == '-'):
    newstring += '"-"'
    continue
  newstring += element

continue is a keyword making it skip the rest of the loop and continue with next iteration.

If this doesn't help I misunderstood your question.

To keep track of what letters have been guessed yes you need to use a separate string, when I said you don't have to keep track I was referring to part 6 clearly stating you only have 6 guesses (doesn't mention anything about right or wrong), and if you have tried run my example you will see why I put a hmmm in with that :).

This kind of helps... I used the quotations to show what I was trying to print and stuff... they're not part of the actual program... I'll give it a try and get back to you.

I believe I am in the same class as Her-o!
I'm a bit further along, but not much - perhaps someone can help me. Here's my issue:
Part of our program requires us to represent letters in the phrase to be guessed with "_", spaces with " ", dashes with "-", and apostrophes with "'".

Here's the code so far:

# Project 4
# Hangman Program
# KDW 2008

import string

print 'Welcome.  You have elected to play Hangman, a popular word and phrase game.  You will be prompted for a phrase to guess (have a friend enter one if you want to be surprised).  You will then guess letters until you have completed the phrase, or your missed chances run out.  You are allowed a total of six (6) missed guesses.  Good luck.'

word = raw_input('Enter a word to guess: ')
chances = 6
guesses = ''
incorrect = ''

while chances > 0:
    missed = 0
    for letter in word:
        if letter in guesses:
            print letter,
        else:
            print '_',
            missed += 1
        
    print
    print 'Incorrect guesses so far:',incorrect,
    print
    
    if missed == 0:
        print 'Winner!'
        break

    guess = raw_input('Guess a letter: ')
    guesses += guess
    
    if guess not in word:
        chances -= 1
        incorrect += guess
        print 'Sorry, try again.'
        print chances, 'chances left.'
        if chances == 0: print 'The answer is',word,'.'

How would I replace (space) in the "word" input with a literal " "? Same with the other special characters?

Thanks,
Kyle

Hey since I'm a newbie and all can you help explain how you were able to get the dashes and the letters to print and remove the dashes? I can't seem to get it right.

Okay so i got the dashes and letters working but I have one problem, that is that when there is only one letter left to solve and when the correct letter is guessed it doesn't show up in the dashes instead it goes straight to the Gongrats you won statement. can anyone help me?

I finally got this working! The if (letter == ' '): did the trick, with a slight modification to a couple other parts of the program. I added a few finishing touches and turned it in! Thanks so much for your help! Her-o, email me, buddy -- we'll get you on the right track so you can Hand in before midnight! opsryushi at gmail dot com.

hi can you give me a simple code of lucky 9 game in python?this game is a card game if you get a number 9 you will be the winner!i dont have idea how to create this program can you teach me plz!!!!!

Editor's note:
Please post your own thread, do not hijack unrelated threads!

no help for zombie master homework kiddos

no help for homework kiddos and zombie masters

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.