Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hangman Troubleshooting
#1
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
Reply
#2
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.
Reply
#3
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')
Reply
#4
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
Reply
#5
Thanks for the information!
Reply
#6
Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  hangman doesn't end after guessing the word vinci 3 1,849 Nov-06-2019, 09:51 PM
Last Post: ichabod801
  fun with hangman! Low_Ki_ 7 6,906 Jun-13-2017, 05:03 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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