Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hangman Help.
#1
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!")
Reply
#2
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.
Reply
#3
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
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.
Reply
#5
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Hangman metro17 4 2,975 Sep-18-2019, 10:59 AM
Last Post: perfringo
  Trouble coding hangman Tay 1 2,331 Mar-28-2019, 01:57 AM
Last Post: ichabod801
  Hangman code problem KrakowKid 1 2,372 Feb-25-2019, 06:29 PM
Last Post: ichabod801
  Python hangman help A1395 11 6,977 Feb-13-2019, 04:24 PM
Last Post: ichabod801
  Hangman 2skywalkers 3 60,748 Oct-19-2018, 01:49 PM
Last Post: ichabod801
  Python Hangman Replacing "_" with letters. 2skywalkers 6 11,982 Jun-25-2018, 12:01 PM
Last Post: gruntfutuk
  Simple Hangman Game Issue andrew95 2 4,306 Apr-02-2018, 02:24 PM
Last Post: andrew95
  Designing Hangman game spacetimeguy 2 5,085 Feb-02-2018, 08:55 PM
Last Post: spacetimeguy
  TKinter Hangman Game Bumble 1 20,308 Jul-19-2017, 06:56 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020