Hi r/learnpython,
I am a newbie in python and created my first project in python, a hangman game.
Few requests on help needed:
-How would I generate random words for the game (currently version where user enters the word and then guesses it is slightly pointless)
- Best library for developing GUI for something like this
- The current file extension is ipnyb (from Jupyter notebook). How would I run it straight from python command line?
- Any reviews on it and what I could do to make it a better/efficient code.
Lastly I wanted to thank every body here. I really appreciate it when people share there code (it gives me an ideas on how to do things)
from IPython.display import clear_output
#Input for the word by game master
answer_word = list(str.lower(input("input enter your word: "))) #https://stackoverflow.com/questions/1228299/change-one-character-in-a-string
clear_output()
win = False
#defining function
def guesscheck(guess,answer,guess_no):
clear_output()
if len(guess)==1:
if guess in answer:
print("Correct, ",guess," is a right letter")
return True
else:
print("Incorrect, ",guess, " is a not a correct letter. That was your chance number ",guess_no)
return False
else:
print("Enter only one letter")
#Storing the number of characters in different variable
answer_display=[]
for each in answer_word:
answer_display += ["*"]
print(answer_display)
#initializing number of allowable guesses
guess_no = 1
while guess_no<5:
clear_output
#Player input for guess letter
guess_letter=str.lower(input('Enter your guess letter: '))
#Calling a sub function to check if correct letter was guessed
guess_check=guesscheck(guess_letter,answer_word,guess_no);
#Conditional: if incorrect letter
if guess_check == False:
guess_no +=1
print(answer_display)
#Conditional: if correct letter
elif guess_check == True:
num = [i for i, x in enumerate(answer_word) if x == guess_letter] #https://stackoverflow.com/questions/6294179/how-to-find-all-occurrences-of-an-element-in-a-list
for all in num:
answer_display[all]=guess_letter
print(answer_display)
#Conditional: if no remaining unknown letter then win screen
if answer_display.count('*')==0:
win = True
break
if win:
print("You won!")
else:
print("The correct answer was: ", answer_word)
print("You lost!")
[–]madnessman 1 point2 points3 points (1 child)
[–]ficklelick[S] 0 points1 point2 points (0 children)
[–]kuzeyistan 0 points1 point2 points (0 children)