我在Python 3.x工作并且相当新,所以我希望我所要求的是有道理的 . 我应该专注于这个猜词游戏的循环和字符串 . 这是我到目前为止的(混乱/冗长/混乱)代码:
import sys
import random
def Main():
PlayAgain = "y"
print("COP 1000 Project 4 - Courtney Kasonic - Guess the Word Game")
print("I'm thinking of a word; can you guess what it is?")
while PlayAgain == "y":
Words = "apple alphabet boomarang cat catharsis define decide elephant fish goat horizon igloo jackelope ketchup loop limousine monkey night octopus potato quick rebel separate test underway violin world yellow zebra".split()
SecretWord = random.choice(Words)
MissedLetters = ""
CorrectLetters = ""
ChosenWord = GetWord(Words)
Guess = FiveLetters(CorrectLetters+MissedLetters)
for Guess in ChosenWord:
CorrectLetters = CorrectLetters + Guess
ShowWord(CorrectLetters, ChosenWord)
for i in ChosenWord:
CLetters = ""
if Guess in ChosenWord:
Blanks = "_" * len(SecretWord)
for i in range(len(SecretWord)):
if SecretWord[i] in CLetters:
Blanks = Blanks[i] + SecretWord[i]
print(Blanks)
print(CLetters)
def GetWord(List):
SecretWord = random.choice(List)
return(SecretWord)
**def FiveLetters(LettersGuessed):
a = 2
if a > 1:
print("Enter five letters to check: ",end="")
Guess = input()
if len(Guess) != 5:
print("Please enter five letters.")
elif Guess in LettersGuessed:
print("You already guessed that letter.")
elif Guess not in "abcdefghijklmnopqrstuvwxyz":
print("Please enter a letter.")
else:
return(Guess)**
def ShowWord(CLetters, SecretWord):
print("\nHere is the word showing the letters that you guessed:\n")
CLetters = ""
Blanks = "_" * len(SecretWord)
for i in range(len(SecretWord)):
if SecretWord[i] in CLetters:
Blanks = Blanks[i] + SecretWord[i]
print(Blanks)
print(CLetters)
return(Blanks, SecretWord, CLetters)
def CheckLetters(Letters):
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".split()
for Letters in Word:
print(Letters)
return(Letters)
Main()
粗体区是我遇到问题的地方 . 只能输入五个字母来“检查”以查看它们是否在保密字中 . 它只接受像“abcde”这样的输入 . 它不接受像“aaaaa”或“muihi”这样的输入,即它不接受不按顺序或具有多个相同字母的猜测 .
我也有下划线的问题 . 不确定我的代码是否正确 . 正确猜测的字母不会取代适当的下划线 .
例如:秘密词=狗 . 如果我猜字母“mopfe”(虽然因为上面的问题我不能)然后它会打印出“_ _ _”而没有“o” .
1 回答
假设您不想失败,您将不可避免地提出更多问题 . 如果你在StackOverflow上问这些问题,你应该read the FAQ . 由于您使用的是python,您可能还应该阅读样式指南(即PEP 8),评论中提到了它 . 此代码虽然不完整,但可以帮助您入门 .