Python Forum
Hangman Troubleshooting - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Game Development (https://python-forum.io/forum-11.html)
+--- Thread: Hangman Troubleshooting (/thread-24644.html)



Hangman Troubleshooting - Ewilliam51 - Feb-24-2020

Okay so, I created a hangman game, this is the code for the game

import time
name = input("what is your name?: ")
print("Hello, " +name +" , time to play hangman")
time.sleep(2)
print("This word has 4 letters, you will have to guess all 4 letter, \neven repeats, in order to win")
print (" ")
time.sleep(2)

word = 'test'
guesses =" "
turns = 10
correct = 0
while turns > 0:
		guess = input("guess a character: \n")
		guesses += guess
		if guess in word:
			correct += 1
			print("correct")
			print("You have " + str(correct) +  ' out of ' + str(len(word)) + "letters" )
		elif guess not in word: 
			turns -= 1
			print ("wrong") 
			print ("you have", + turns, ' more guesses')
		if correct == 4:  
			print("You win! \nthe word was test.")
			break
		elif turns == 0:
			print ("game over")
			break
and just like hangman the program asks the user for a character, if the letter they guess is in the word they gain a point, and if the letter they use is not in the game then they lose a turn. If you reach as many points as the letters in the word you win, (for example the word I used is test and there are 4 letters in that so when the user guesses the 4 letters correctly they win),

but I have 1 dilemas(actually I have quite a few but I'm only focusing on these one)
I have found that if the user clicks enter without typing in a letter the program still marks that as correct and gives the person a point,
Is there anyway that I can stop this, I'm not sure,if their is but I figured I would ask anyway


RE: Hangman Troubleshooting - SheeppOSU - Mar-22-2020

Just check the input, here's some simple code for an example. You can turn this into a function to make the code smoother.
import time
letters = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}

something = input("Type a letter: ")
while something not in letters:
    print("I said type *a letter*")
    time.sleep(1)
    something = input("Type something: ")
I just put the wait in there so that there's space between the print and input. So, the code above as you can see, will check to make sure that they are typing 1 letter, so no multiple letters, no numbers, etc.


RE: Hangman Troubleshooting - joe_momma - Mar-27-2020

you could also:
>>> guess
''
>>> if guess:
	    print('true') #then make a check it's a legal character or already guessed one
    else:
        guess= input('enter a character')



RE: Hangman Troubleshooting - edwinley - Jul-06-2020

import time
letters = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}

something = input("Type a letter: ")
while something not in letters:
    print("I said type *a letter*")
    time.sleep(1)
    something = input("Type something: ")
slitherio


RE: Hangman Troubleshooting - Sooqa - Jul-21-2020

Thanks for the information!


RE: Hangman Troubleshooting - Sooqa - Jul-22-2020

Smile