为基本单词混乱游戏添加提示

我正在从一本书中学习Python,这是《绝对初学者》的《 Python编程》. 现在,我正在其中一章的结尾处挑战挑战. 在章节中,我经历了一个单词杂耍游戏的制作. 最后,我被要求添加每个单词的提示,并添加一个评分系统,以奖励他猜测的每个单词的播放器,而无需使用提示. 我真的不确定最好的方法. 任何建议和技巧都将不胜感激! 这是游戏的代码:

选择 | 换行 | 行号
  1. # Word Jumble
  2. #
  3. # The computer picks a random word then "jumbles" it
  4. # The player has to guess the original word
  5.  
  6. import random
  7.  
  8. # create a sequence of words to choose from
  9. WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone",
  10.          "anion", "cation", "polymorphasis", "stipulation", "antecedant")
  11.  
  12. # pick one word randomly from the sequence
  13. word = random.choice(WORDS)
  14. # create a variable to use later to see if the guess is correct
  15. correct = word
  16.  
  17. # create a jumbled version of the word
  18. jumble =""
  19.  
  20. while word:
  21.     position = random.randrange(len(word))
  22.     jumble += word[position]
  23.     word = word[: position] + word[(position + 1):]
  24.  
  25. # start the game
  26. print \
  27. """
  28.              Welcome to Word Jumble!
  29.  
  30.     Unscramble the letters to make a word.
  31. (Press the enter key at the prompt to quit.)
  32. """
  33. print "The jumble is:", jumble
  34.  
  35. guess = raw_input("\nYour guess: ")
  36. guess = guess.lower()
  37. while (guess != correct) and (guess != ""):
  38.     print "Sorry, thats not it."
  39.     guess = raw_input("Your guess: ")
  40.     guess = guess.lower()
  41. if guess == correct:
  42.     print "That's it! You guessed it!\n"
  43.  
  44. print "Thanks for playing"
  45.  
  46. raw_input("\n\nPress the enter key to exit.")
  47.  

感谢您的时间和帮助!

# 回答1

我已经在您的帖子中添加了代码标签,以便其他人可以看到您的代码结构. 您可以通过阅读页面右侧的"发布准则"或"回复指南"(在发布或回复时)来学习如何执行此操作. 谢谢.
# 回答2

好的,对不起. 我没有看到那个. 我认为可能有类似的东西,但我不确定. 现在我知道了! 谢谢.
# 回答3

使用一系列长度符合单词长度的长度启动提示字符串. 提示用户输入问号或提示的任何内容. 作为提示,随机选择一个以正确位置显示的字母,并在该位置更换下划线. 扣除用户所需的每个提示的点或点. 这样的事情:

选择 | 换行 | 行号
  1. The jumble is: tufdilcfi
  2. Hint string: _________
  3. Hint string: ___f_____
  4. Hint string: ___f____t

hth :)

# 回答4

感谢您的建议. 抱歉,我花了很长时间才能回到这个. 那听起来像一个好主意. 我不确定该怎么做. 我如何将该过程集成到现有代码中并将这些提示附加到单词中? 这对您来说似乎真的很基本,但是我想我真的很新. 谢谢你的一切!
# 回答5

如何测试像""这样的角色? 这表明用户需要提示. 例如,当用户输入"?"时,循环循环循环时,将打印正确的答案. 只需编写一个函数,该函数以您喜欢的任何形式提供提示,然后将其替换为" PRINT正确"语句.

选择 | 换行 | 行号
  1. while (guess != correct) and (guess != ""):
  2.     if guess =='?':
  3.         print correct
  4.         guess = raw_input("Your guess: ")
  5.         guess = guess.lower()
  6.     else:
  7.         print "Sorry, thats not it."
  8.         guess = raw_input("Your guess: ")
  9.         guess = guess.lower()
  10.  
# 回答6

这是未经测试的,因此可能行不通,但也许您会得到这个想法:

选择 | 换行 | 行号
  1. guess = ""
  2. lst = range(len(jumble))
  3. hint_str = '_'*len(jumble)
  4. while True:
  5.     guess = raw_input("Guess or '?' or 'X': ").lower()
  6.     if guess == correct:
  7.         print "That's it! You guessed it!\n"
  8.         break
  9.     elif guess == '?':
  10.         i = random.choice(lst)
  11.         lst.remove(i)
  12.         hint_str[i] = guess[i]
  13.         print hint_str
  14.     elif guess == 'x':
  15.         print "Sorry you gave up!"
  16.         break
  17.     else:
  18.         print "Sorry, thats not it. Try again."
  19.  
  20. print "Thanks for playing"
  21.  
  22. raw_input("\n\nPress the enter key to exit.")
# 回答7

我看一下. 它确实有一些我尚未引入的东西,例如"删除"和"随机". 看起来不错,谢谢! 也要感谢Dshimer.
# 回答8

嘿,大家. 我从春假开始回来,我决定应该完成. 因此,我看了大家提出的建议,它们很好,除了一部分没有工作,并且通过一些调整,这就是我想到的:

选择 | 换行 | 行号
  1. # Word Jumble
  2. #
  3. # The computer picks a random word then "jumbles" it
  4. # The player has to guess the original word
  5.  
  6. import random
  7.  
  8. # create a sequence of words to choose from
  9. WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone",
  10.          "anion", "cation", "polymorphasis", "stipulation", "antecedant")
  11.  
  12. # pick one word randomly from the sequence
  13. word = random.choice(WORDS)
  14. # create a variable to use later to see if the guess is correct
  15. correct = word
  16.  
  17. # create a jumbled version of the word
  18. jumble =""
  19.  
  20. while word:
  21.     position = random.randrange(len(word))
  22.     jumble += word[position]
  23.     word = word[:position] + word[(position + 1):]
  24.  
  25. # sets score to zero
  26. score = 0
  27.  
  28. # start the game
  29. print \
  30. """
  31.              Welcome to Word Jumble!
  32.  
  33.     Unscramble the letters to make a word.
  34. Enter a guess, an X to give up, or type ? and press enter for a hint.
  35. (Press the enter key at the prompt to quit.)
  36.  
  37. Try to get the lowest score possible. For each hint, you gain a point.
  38. See if you can win with no points!
  39. """
  40. print jumble
  41. guess = ""
  42. lst = range(len(jumble))
  43. hint_str = '_'*len(jumble)
  44. while True:
  45.     guess = raw_input("Guess or '?' or 'X': ").lower()
  46.     if guess == correct:
  47.         print "That's it! You guessed it!\n Your score is", score
  48.         break
  49.     elif guess == '?':
  50.         i = random.choice(lst)
  51.         print correct[i], "is the", i+1, "letter."
  52.         score += 1
  53.     elif guess == 'x':
  54.         print "Sorry you gave up!"
  55.         break
  56.     else:
  57.         print "Sorry, thats not it. Try again."
  58.  
  59. print "Thanks for playing"
  60.  
  61.  
  62. raw_input("\n\nPress the enter key to exit.")
  63.  

感谢您的帮助!

标签: python

添加新评论