Hi,
I'm trying to make a hangman game that should look like this:
Welcome to Hangman
______
Your guess: c
Success!
__cc__
Your guess: b
Failure! You have tried tried 1 times!
__cc__
.
.
.
Your guess: r
Success!
soccer
You won!
etc
In this game I'm trying to make it so that I first need to give a file - where game takes words - at commandline as a parameter to game. I've done that so far that program takes randomly a word from a certain file (here words.txt), prints blanks as a hintword and starts asking letters. My problem is that I can't make the program to check for the letters in word and replace blanks with guessed letter.
So far my code looks like that:
Can anyone tell me how to solve this problem? Sorry for the dots, but I didn't know any other way to get that structure showing right.
Edit: Thanks for the tip. I edited the code to show right :)
I'm trying to make a hangman game that should look like this:
Welcome to Hangman
______
Your guess: c
Success!
__cc__
Your guess: b
Failure! You have tried tried 1 times!
__cc__
.
.
.
Your guess: r
Success!
soccer
You won!
etc
In this game I'm trying to make it so that I first need to give a file - where game takes words - at commandline as a parameter to game. I've done that so far that program takes randomly a word from a certain file (here words.txt), prints blanks as a hintword and starts asking letters. My problem is that I can't make the program to check for the letters in word and replace blanks with guessed letter.
So far my code looks like that:
Code:
import sys
if sys.argv[1] == 'words.txt':
list = open('words.txt','r')
a = list.readlines()
import random
b = random.choice(a)
print 'Welcome to Hangman'
c = '_'*(len(b)-1)
print c
for i in range(1,6):
mark = raw_input('Your guess: ')
check = b.find(mark)
if check != -1:
print 'Succes!', mark, 'is in word at position', check
c = c[:check] + mark + c[check + 1:]
print c
else:
print 'Failure! You have tried tried %d times!' %(i)
else:
print '[Errno2] No such file or directory: %s' %(sys.argv[1])
Edit: Thanks for the tip. I edited the code to show right :)
Comment