我正在从一本书中学习Python,这是《绝对初学者》的《 Python编程》. 现在,我正在其中一章的结尾处挑战挑战. 在章节中,我经历了一个单词杂耍游戏的制作. 最后,我被要求添加每个单词的提示,并添加一个评分系统,以奖励他猜测的每个单词的播放器,而无需使用提示. 我真的不确定最好的方法. 任何建议和技巧都将不胜感激! 这是游戏的代码:
- # Word Jumble
- #
- # The computer picks a random word then "jumbles" it
- # The player has to guess the original word
-
- import random
-
- # create a sequence of words to choose from
- WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone",
- "anion", "cation", "polymorphasis", "stipulation", "antecedant")
-
- # pick one word randomly from the sequence
- word = random.choice(WORDS)
- # create a variable to use later to see if the guess is correct
- correct = word
-
- # create a jumbled version of the word
- jumble =""
-
- while word:
- position = random.randrange(len(word))
- jumble += word[position]
- word = word[: position] + word[(position + 1):]
-
- # start the game
- print \
- """
- Welcome to Word Jumble!
-
- Unscramble the letters to make a word.
- (Press the enter key at the prompt to quit.)
- """
- print "The jumble is:", jumble
-
- guess = raw_input("\nYour guess: ")
- guess = guess.lower()
- while (guess != correct) and (guess != ""):
- print "Sorry, thats not it."
- guess = raw_input("Your guess: ")
- guess = guess.lower()
- if guess == correct:
- print "That's it! You guessed it!\n"
-
- print "Thanks for playing"
-
- raw_input("\n\nPress the enter key to exit.")
-
感谢您的时间和帮助!
# 回答1 我已经在您的帖子中添加了代码标签,以便其他人可以看到您的代码结构. 您可以通过阅读页面右侧的"发布准则"或"回复指南"(在发布或回复时)来学习如何执行此操作. 谢谢.
# 回答2 好的,对不起. 我没有看到那个. 我认为可能有类似的东西,但我不确定. 现在我知道了! 谢谢.
# 回答3 使用一系列长度符合单词长度的长度启动提示字符串. 提示用户输入问号或提示的任何内容. 作为提示,随机选择一个以正确位置显示的字母,并在该位置更换下划线. 扣除用户所需的每个提示的点或点. 这样的事情:
- The jumble is: tufdilcfi
- Hint string: _________
- Hint string: ___f_____
- Hint string: ___f____t
hth :)
# 回答4 感谢您的建议. 抱歉,我花了很长时间才能回到这个. 那听起来像一个好主意. 我不确定该怎么做. 我如何将该过程集成到现有代码中并将这些提示附加到单词中? 这对您来说似乎真的很基本,但是我想我真的很新. 谢谢你的一切!
# 回答5 如何测试像""这样的角色? 这表明用户需要提示. 例如,当用户输入"?"时,循环循环循环时,将打印正确的答案. 只需编写一个函数,该函数以您喜欢的任何形式提供提示,然后将其替换为" PRINT正确"语句.
- while (guess != correct) and (guess != ""):
- if guess =='?':
- print correct
- guess = raw_input("Your guess: ")
- guess = guess.lower()
- else:
- print "Sorry, thats not it."
- guess = raw_input("Your guess: ")
- guess = guess.lower()
-
# 回答6 这是未经测试的,因此可能行不通,但也许您会得到这个想法:
- guess = ""
- lst = range(len(jumble))
- hint_str = '_'*len(jumble)
- while True:
- guess = raw_input("Guess or '?' or 'X': ").lower()
- if guess == correct:
- print "That's it! You guessed it!\n"
- break
- elif guess == '?':
- i = random.choice(lst)
- lst.remove(i)
- hint_str[i] = guess[i]
- print hint_str
- elif guess == 'x':
- print "Sorry you gave up!"
- break
- else:
- print "Sorry, thats not it. Try again."
-
- print "Thanks for playing"
-
- raw_input("\n\nPress the enter key to exit.")
# 回答7 我看一下. 它确实有一些我尚未引入的东西,例如"删除"和"随机". 看起来不错,谢谢! 也要感谢Dshimer.
# 回答8 嘿,大家. 我从春假开始回来,我决定应该完成. 因此,我看了大家提出的建议,它们很好,除了一部分没有工作,并且通过一些调整,这就是我想到的:
- # Word Jumble
- #
- # The computer picks a random word then "jumbles" it
- # The player has to guess the original word
-
- import random
-
- # create a sequence of words to choose from
- WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone",
- "anion", "cation", "polymorphasis", "stipulation", "antecedant")
-
- # pick one word randomly from the sequence
- word = random.choice(WORDS)
- # create a variable to use later to see if the guess is correct
- correct = word
-
- # create a jumbled version of the word
- jumble =""
-
- while word:
- position = random.randrange(len(word))
- jumble += word[position]
- word = word[:position] + word[(position + 1):]
-
- # sets score to zero
- score = 0
-
- # start the game
- print \
- """
- Welcome to Word Jumble!
-
- Unscramble the letters to make a word.
- Enter a guess, an X to give up, or type ? and press enter for a hint.
- (Press the enter key at the prompt to quit.)
-
- Try to get the lowest score possible. For each hint, you gain a point.
- See if you can win with no points!
- """
- print jumble
- guess = ""
- lst = range(len(jumble))
- hint_str = '_'*len(jumble)
- while True:
- guess = raw_input("Guess or '?' or 'X': ").lower()
- if guess == correct:
- print "That's it! You guessed it!\n Your score is", score
- break
- elif guess == '?':
- i = random.choice(lst)
- print correct[i], "is the", i+1, "letter."
- score += 1
- elif guess == 'x':
- print "Sorry you gave up!"
- break
- else:
- print "Sorry, thats not it. Try again."
-
- print "Thanks for playing"
-
-
- raw_input("\n\nPress the enter key to exit.")
-
感谢您的帮助!