Python Forum
Hangman Help. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Hangman Help. (/thread-11140.html)



Hangman Help. - 2skywalkers - Jun-24-2018

I have been trying to make hangman but I have not been able to figure it out. I found a video and copied the code down so I could then figure out whats going on and then write my own version, but, the code does not work and I'm not sure what is happening. When I run it in BBEdit no matter what letter I put in, it just prints the underscores and The "guess a letter" part.

import random

possibleAnswers = ["dog","human","bed"]

random.shuffle(possibleAnswers)

answer = (possibleAnswers[1])


display = []
display.extend(answer)


for i in range(len(display)):
	display[i] = ("_")
	

print(" ".join(display))

count = 0

while count < len(answer):

	guess = input("Please guess a letter: ")
	
	guess = guess.upper()
	
	
	for i in range(len(answer)):
	    if answer[i] == guess:
		    display[i] = guess
			count += 1
			
	
	print(" ".join(display))
	
print("You guessed it!")



RE: Hangman Help. - Larz60+ - Jun-24-2018

after line 24, add:
guess.strip()
remove line 26

There are more issues, the number of guesses should be more than the length of the word, otherwise all letters would have to be guessed without error.


RE: Hangman Help. - ichabod801 - Jun-25-2018

Actually, you want to remove this line:

guess = guess.upper()
This upper cases the letter you guessed, so if you guessed 'a', it becomes 'A'. But if you look at possibleAnswers at the beginning of the code, it's all in lower case. So you're constantly trying to match upper case characters to lower case. It would actually make more sense to replace that line with:

guess = guess.lower()
That way, even if you guess 'A', it will match against 'a', which is what might be in the answer.

BTW, if that really is the code you copied from the tutorial you are learning from, find a new tutorial. That's really bad code. Oh, and count is the number of correct answers, so what the code actually does is let you guess until you have all the letters. You can't lose.


RE: Hangman Help. - 2skywalkers - Jun-25-2018

Thanks for the help iceabod801, I have had trouble finding good examples of hangman games that I can understand, if you have any suggestions I would love to know please. And also, how could I make this code better? Thanks.


RE: Hangman Help. - ichabod801 - Jun-26-2018

First of all, you don't need to shuffle and then pick an item from the list. The random module can do both at once with the choice function

>>> nums = [8, 0, 1]
>>> random.choice(nums)
0
>>> random.choice(nums)
8
Next, looping over range(len(whatever)) is looping over the indexes of a list. It is much better to loop over the list itself.

>>> for num in nums:
...     print(num)
...
8
0
1
But you don't even need to do that to set up display. You can multiply a list to extend it multiple times.

>>> [801] * 8
[801, 801, 801, 801, 801, 801, 801, 801]
Note that this works fine with numbers and strings, but can cause problems with other objects. Don't do this with lists.

See how he prints before the loop and at the end of the loop? Try switching it to print just at the start of the loop.

In a real hangman game, you lose on the sixth wrong guess. So I would start with count = 6, and then subtract one each time there is a wrong guess (each time guess not in answer). You loop would be while count, which will loop until count == 0. You will also need to break out of the loop if they guess correct("_" not in display). The break statement jumps out of the loop.

Finally, there is his for loop inside the while loop, again with range(len(something)). Here, the zip function is very useful. It its most basic usage, it takes two lists and returns a sequence of a pair of the first item in each list, a pair of the second item in each list, and so on.

>>> count = [1, 2, 3]
>>> list(zip(nums, count))
[(8, 1), (0, 2), (1, 3)]
>>> for num, ordinal in zip(nums, count):
...     print(num + ordinal)
...
9
2
4
Combine this with the common technique of starting with an empty list and appending to it each time through the loop.

>>> sums = []
>>> for num, ordinal in zip(nums, count):
...     sums.append(num + ordinal)
>>> sums
[9, 2, 4]
That's a lot I just dumped on you. I would take it one piece at a time, make the appropriate change to his code, and then make sure the program still works before moving on to the next piece.