我想知道是否还有其他方法可以解决我的情况(在我的Hangman游戏中将用户输入(guessword)替换为破折号字符串(单词)而不是使用global。我以前的游戏帖子:How to replace the repeat word in python (Hangman Game)!和How do i detect the repeat input in my hangman game (Python)!这是我的代码: 代码从开始:
def writeCustomwords():
f = open('custom.txt', 'w')
words = input("Enter a list of comma separated words:")
print("Custom words save into custom.txt")
f.write(words)
f.close()
def readFileWords():
if choose == "1":
txt = open('easy.txt', 'r')
txt.read()
txt.close()
elif choose == "2":
txt = open('intermediate.txt', 'r')
txt.read()
txt.close()
elif choose == "3":
txt = open('hard.txt', 'r')
txt.read()
txt.close()
elif choose == "4":
txt = open('custom.txt', 'r')
txt.read()
txt.close()
import random
def getRandomWord():
if choose == "1":
with open('easy.txt') as txt:
word = txt.read().splitlines()
random_word = random.choice(word)
print(random_word)
print("\nThere are", len(random_word),"letters in this word")
elif choose == "2":
with open('intermediate.txt') as txt:
word = txt.read().splitlines()
random_word = random.choice(word)
print("\nThere are", len(random_word),"letters in this word")
elif choose == "3":
with open('hard.txt') as txt:
word = txt.read().splitlines()
random_word = random.choice(word)
print("\nThere are", len(random_word),"letters in this word")
elif choose == "4":
with open('custom.txt') as txt:
word = txt.read().split(",")
random_word = random.choice(word)
print(random_word)
print("\nThere are", len(random_word)-random_word.count(" "),"letters in this word")
return random_word
def gethiddenWord():
import re
guess = getRandomWord().lower()
guessed = re.sub(r"\S", "-", guess)
print("\n\t\t\t-----------------------------------------------")
print("\t\t\tRULES")
print("\t\t\t-----------------------------------------------")
print("\n\t\t\tPlease Guess one letter(a-z) at a time!")
print("\n\t\t\tIf you want to guess the word, please enter '0'")
print("\n\nThe word is : ",guessed)
return guess,guessed
def checkValidGuess():
if len(guessword)==1 and guessword not in guesslist and guessword not in num:
return True
elif guessword in guesslist:
print("You have already guessed that letter")
elif guessword in num:
print("You can only input letter a-z")
print("Try again")
elif len(guessword) >1:
print("You can only guess one letter at a time!")
print("Try again")
def checkPlayerWord():
if guessall == word:
return True
else:
return False
return checkp
def checkLetterInWords():
if guessword.lower() in word:
return True
elif guessword.lower() not in word and guessword.lower() not in num:
return False
def gethiddenWord():
import re
guess = getRandomWord().lower()
guessed = re.sub(r"\S", "-", guess)
print("\n\t\t\t-----------------------------------------------")
print("\t\t\tRULES")
print("\t\t\t-----------------------------------------------")
print("\n\t\t\tPlease Guess one letter(a-z) at a time!")
print("\n\t\t\tIf you want to guess the word, please enter '0'")
print("\n\nThe word is : ",guessed)
return guess,guessed
我的问题在这里:
def getGuessedWord():
global words
for pos,l in enumerate(word):
if l==guessword.lower():
words= words[:pos] + guessword.lower() +words[pos+1:]
print(words)
return words
在功能之外
word,words = gethiddenWord()
我的访问功能的进一步代码:
choose = input("Enter your choice:")
readFileWords()
time =10
word,words = gethiddenWord()
count = len(word)-word.count(" ")
guesslist=[]
while time !=0 and word:
print("You have", time, "guesses left.")
guessword = input("Guess a letter or enter '0''to guess the word: ")
num = ["1","2","3","4","5","6","7","8","9"]
valid = checkValidGuess()
guesslist.append(guessword.lower())
if guessword =="0":
guessall = input("What is the word: ")
checkp = checkPlayerWord()
del guesslist[0]
if checkp == True:
print("Well done! You got the word correct!")
break
elif checkp == False:
print("Uh oh! That is not the word!")
time -=1
elif valid== True:
checkl= checkLetterInWords()
if checkl == True:
time -=1
print("Well done!",guessword,"is in my word")
count -= word.count(guessword)
getGuessedWord()
countfinish = checkFinish()
if countfinish == True:
break
else:
continue
elif checkl == False:
time -=1
print("Uh oh! That letter is not in my word!")
如果没有全球性,我无法解决它。请帮忙!感谢