【问题标题】:Finding the index by searching for a string in a list通过在列表中搜索字符串来查找索引
【发布时间】:2013-07-28 01:39:38
【问题描述】:

我目前正在阅读“Python Programming for the Absolute Beginner ed 3”,我对其中一个挑战有疑问。

我正在创建一个 Word Jumble 游戏,它会从列表或元组中挑选一个单词,将这个单词打乱并让用户猜这个单词。

# Word Jumble

# The computer picks a random word and 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", "difficulty", "answer", "xylophone")

# 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 = input("\nYour guess: ")
while guess != correct and guess != "":
    print("Sorry, that's not it.")
    guess = input("Your guess: ")

if guess == correct:
    print("That's it!  You guessed it!\n")

print("Thanks for playing!")
input("\n\nPress the enter key to exit.")

这是书中的原始代码。挑战在于在游戏中实施提示和评分系统。我有一个想法,即创建另一个与 WORDS 元组相对应的元组,并在那里有提示。即:

hints = ("*insert hint for python*",
         "*insert hint for jumble*",
         "*insert hint for easy*",
         "*insert hint for difficulty*",
         "*insert hint for answer*",
         "*insert hint for xylophone*")

我想做的是找到 random.choice 词的索引,所以这是我尝试的。

index = word.index(WORDS)
print(index)

我在想这会返回 WORDS 元组的整数,并允许我使用以下方法打印提示:

print(hints[index])

但是,我错了。这可能吗?我让它工作了,但它是一个长长的 if、elif 语句列表,例如:

if guess == "hint" or guess == "Hint" or guess == "HINT":
    if hint == "python":
        print(HINTS[0])

我知道有些人可能会说,“既然它有效,为什么不坚持下去呢?”我知道我可以做到这一点,但我学习 Python 或一般编程的目的是知道如何以各种方式完成设定的任务。

--这部分是次要的,不需要回复,除非你想--

另外我的评分系统如下,以防大家有什么想法可以改进,或者做得好不好。

这个想法是您的分数从 100 开始,如果您使用提示,您将失去总分的 50%。每次猜测都会从总分中扣除 10 分。如果您的分数达到负数,它将被设置为 0。这是我的做法。

score = 100
guesses = 1

这是在使用提示后添加的。

score //= 2

猜测之后。

guesses += 1

最后如果猜对了。

if guess == correct:
print("That's it!  You guessed it!\n")
score = score - (guesses - 1) * 10
if score <= 0:
    score = 0
print("\nYour score is: ", score)

与往常一样,非常感谢任何帮助。

【问题讨论】:

  • 那么你的问题是什么?

标签: python algorithm python-3.x


【解决方案1】:

如果你有:

>>> WORDS = ("python", "jumble", "easy", "difficulty", "answer", "xylophone")

而你使用index 方法,你会得到该单词在列表中的数字位置:

>>> WORDS.index('easy')
2

同样:

>>> word = random.choice(WORDS)
>>> word
'answer'
>>> WORDS[WORDS.index(word)]
'answer'

您在问题中建议您看到一些没有意义的行为。如果您认为您正在做的事情与我在此处说明的内容非常相似,那么如果您可以用一个特定示例更新您的问题,这将有所帮助,该示例显示(a)您期望得到什么,(b)您实际上是什么获取,以及 (c) 在此过程中遇到的任何错误。

【讨论】:

  • 大声笑,我只是把单词和单词放在错误的位置。谢谢!
  • 我很抱歉没有把错误说清楚,我忘了我没有复制/粘贴它。它在说:TypeError: Can't convert 'tuple' object to str implicitly
【解决方案2】:

要从WORDS 获取单词的索引,请使用:

>>> WORDS.index(word)

【讨论】:

  • 哇...哈哈。我不敢相信事情就这么简单。我只是把单词和单词放在了错误的位置。
  • @mccdlibby 发生在我们最好的人身上 :) 祝你学习 Python 好运!
猜你喜欢
  • 1970-01-01
  • 2013-03-23
  • 2015-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-01
  • 1970-01-01
相关资源
最近更新 更多