I really need help with this...i'm trying to make a game that where user guesses a word and it tells the user
1: the right number of letter in the right place
2: the number of letter that is right in the wrong place
but when i test it out,
when the secret word is myrrh
my guess: yrmhr
it will say :
3 correct letter(s) in the wrong place
0 correct letter(s) in the correct place
(which is wrong)
======Heres the code=====
import randomWord
def replaceAt(string, index, character):
return string[:index] + character + string[index+1:]
def getSecretWord():
wordLength = str(raw_input("\nEnter length of word (4 to 13): "))
while not (wordLength.isdigit()) or not int(wordLength)>3 or not int(wordLength)<14:
print "That is not a number between 4 to 13!!"
wordLength = str(raw_input("Enter length of word (4 to 13): "))
print "\n"
secretWord = randomWord.getRandomWord()
wordLength= int(wordLength)
while len(secretWord) != wordLength:
secretWord=randomWord.getRandomWord()
return secretWord
def creatHyphenString(secretWord):
hyphenString = ""
for i in range(len(secretWord)):
hyphenString = hyphenString + "-"
return hyphenString
answer = "y"
score = 0
while answer == "y": # will continue as long as the user want to play again
secretWord = getSecretWord()
secretWordLength = len(secretWord)
print secretWord
hyphenString = creatHyphenString(secretWord)
print hyphenString
wrongGuesses = 0
a = False
while a == False and wrongGuesses < 10:
guess = raw_input ("\nMake a guess: ")
guessLength = len(guess)
while not len(guess) == len(secretWord): # will run as long as its not same length
print "That is not the same length of the secret word!"
guess = raw_input ("\nMake a guess again: ")
##### need check alpha!!
countrightplace = 0
countwrongplace = 0
copysecretWord=secretWord
copyguess = guess
if str(copyguess)!=str(copysecretWord):
for i in range(len(copyguess)): # = 01234
if (copyguess[i] == copysecretWord[i]):
countrightplace = countrightplace+1
copysecretWord = copysecretWord[:i]+ "-" + copysecretWord[i+1:]
copyguess = copyguess[:i] + " " + copyguess[i+1:]
print copyguess
print copysecretWord
for i in range(len(copyguess)):
f = copysecretWord.find(copyguess[i])
if f >= 0:
countwrongplace = countwrongplace+1
copysecretWord = copysecretWord[:f]+ "-" + copysecretWord[f+1:]
copyguess = copyguess[:f] + " " + copyguess[f+1:]
print copyguess
print copysecretWord
print "\n", countwrongplace, "correct letter(s) in the wrong place"
print countrightplace, "correct letter(s) in the correct place"
wrongGuesses = wrongGuesses+1
print "You have", 10-wrongGuesses, "guesses remaining."
else:
a=True
if guess == secretWord:
print "\nYou win!"
score = score + 1
print "Your total score is:", score, "!"
else:
print "\nYou lose."
print "The secret word was: ", secretWord
print "Your total score is:", score ,"!"
answer = raw_input ("\nPlay again? (y/n): ")
I also tried to add the part where it checks user input if its all letters.. but it keeps erroring when i add that
If someone can pleaseee help me with this, it would be great!!! I'm not good with programing, please help me!! thanks!!