I've seen several versions of Hangman game written in many different computer languages like Pascal, C++, Java and even in my TI calculator ( I don't know what the code mean, but what kind of computer languages developers use). I'm really interesting in written this game using Python, however, I don't know how to start. Can someone help me write the simple version of this game with just a few secret words (without the picture of the man is being hanged ^_^)?
write a Hangman game
Collapse
X
-
-
Well, that search expired. Here is one of the longest threads on the subject.Originally posted by bartoncHangman Search on the Python Forum. If you hurry, you'll see the list before the search expires.Comment
-
The codes people used in those threats were so complex. So, I need to break the whole thing into small pieces and work on each one.Originally posted by bartoncHangman Search on the Python Forum. If you hurry, you'll see the list before the search expires.
My first question is how to reveal the * which representes for a hidden letter.
[code=python]
#when the program it running
>> play('python')
* * * * * *
the hidden word has 6 letters
turn: 5
Please enter a letter: p
#the program should print out
You're right
p * * * * * # I don't know how to write this
turn: 5
Please enter a letter: s
#the program should print out
You're wrong
p * * * * * # I don't know how to write this
turn: 4
[/code]Comment
-
Ok you need to check if the letter is in the word and then if it is replace the star in the position of the letter with the actual letter:Originally posted by python101The codes people used in those threats were so complex. So, I need to break the whole thing into small pieces and work on each one.
My first question is how to reveal the * which representes for a hidden letter.
[code=python]
#when the program it running
>> play('python')
* * * * * *
the hidden word has 6 letters
turn: 5
Please enter a letter: p
#the program should print out
You're right
p * * * * * # I don't know how to write this
turn: 5
Please enter a letter: s
#the program should print out
You're wrong
p * * * * * # I don't know how to write this
turn: 4
[/code]
[code=python]
def indexList(s, item, start = 0):
return [i+start for (i, let) in enumerate(s[start:]) if let == item]
base = "******"
word = "python"
letter = raw_input("Ente r a letter: ")
if letter in word:
lbase = list(base)
for pos in indexList(word, letter):
lbase[pos] = letter
base = ''.join(lbase)
[/code]Comment
-
A list is one way. Here's another:[CODE=python]Originally posted by ilikepythonOk you need to check if the letter is in the word and then if it is replace the star in the position of the letter with the actual letter:
[code=python]
def indexList(s, item, start = 0):
return [i+start for (i, let) in enumerate(s[start:]) if let == item]
base = "******"
word = "python"
letter = raw_input("Ente r a letter: ")
if letter in word:
lbase = list(base)
for pos in indexList(word, letter):
lbase[pos] = letter
base = ''.join(lbase)
[/code]
>>> base = "******"
>>> word = "python"
>>> guess = 'h'
>>> idx = word.find(guess )
>>> if idx > -1:
... base = base[:idx] + guess + base[idx + 1:]
...
>>> base
'***h**'
>>> [/CODE]But that won't handle more than one occurrence of the letter in the word.Comment
-
Here is a similar way that will handle multiple occurrences of a letter:[code=Python]def hintStrSub(hint _str, letter, pos_list):
for i in pos_list:
lst = [hint_str[:i], ]
if len(hint_str) >= i+1:
lst.append(hint _str[(i+1):])
hint_str = letter.join(lst )
return hint_str
def indexList(s, item, i=0):
i_list = []
while True:
try:
i = s.index(item, i)
i_list.append(i )
i += 1
except:
break
return i_list[/code]
[code=Python]>>> idx_list = indexList('sequ entially', 'l')
>>> idx_list
[9, 10]
>>> hint = '*' * len('sequential ly')
>>> hintStrSub(hint , 'l', idx_list)
'*********ll*'
>>> [/code]Comment
-
string related question
[code=python]
a = 'hello'
for i in range(len(a)):
print a.index('l',a.i ndex('l')+i)
2
3
Traceback (most recent call last):
File "C:/Python25/test.py", line 4, in <module>
print a.index('l',a.i ndex('l'))
ValueError: substring not found
[/code]
I would like to find all indexes of letter l in the list, I got them but had this error. How can I fix this.Comment
-
This is a neat little function that does what I think you want:Originally posted by python101[code=python]
a = 'hello'
for i in range(len(a)):
print a.index('l',a.i ndex('l')+i)
2
3
Traceback (most recent call last):
File "C:/Python25/test.py", line 4, in <module>
print a.index('l',a.i ndex('l'))
ValueError: substring not found
[/code]
I would like to find all indexes of letter l in the list, I got them but had this error. How can I fix this.
[code=python]
def indexList(s, item, i=0):
i_list = []
while True:
try:
i = s.index(item, i)
i_list.append(i )
i += 1
except:
break
return i_list
[/code]
P.S. Did you take a look at the responses in your other thread? I think they address this same issue.Comment
-
I tried this way:Originally posted by ilikepythonThis is a neat little function that does what I think you want:
[code=python]
def indexList(s, item, i=0):
i_list = []
while True:
try:
i = s.index(item, i)
i_list.append(i )
i += 1
except:
break
return i_list
[/code]
P.S. Did you take a look at the responses in your other thread? I think they address this same issue.
[code=python]
>>name = 'John John'
>>letter = 'J'
>>iList = indexList(name, letter)
>>print iList
[0]
#but it should contain
[0,5]
[/code]Comment
-
thank you very much. I've purchased a python book, it explained very well how to make this game work
[code=python]
so_far = "-" * len(word)
# create a new so_far to include guess
new = ""
for i in range(len(word) ):
if guess == word[i]:
new += guess
else:
new += so_far[i]
so_far = new
[/code]Comment
-
It works for me:[code=Python]>>> indexList("John John", "J")Originally posted by python101I tried this way:
[code=python]
>>name = 'John John'
>>letter = 'J'
>>iList = indexList(name, letter)
>>print iList
[0]
#but it should contain
[0,5]
[/code]
[0, 5]
>>> [/code]Comment
Comment