Py学习  »  Python

如何在python的列表中找到输入的位置[副本]

scarab • 4 年前 • 1003 次点击  

我现在正在构建一个简单的刽子手游戏,我希望能够识别用户在列表中猜测特定字母的位置例如,如果单词列表是[D,R,A,G,O,N]——而用户猜测是A,那么我希望能够得到一个返回值,即4……这是我迄今为止失败的代码

import random

word_list = ['red', 'better', 'white', 'orange', 'time']
hidden_list = []
selected_word = random.choice(word_list)
letters = len(selected_word)
selected_word_list = list(selected_word)
game_playing = True

print('My word has ' + str(letters) + ' letter(s).')

print(selected_word_list)

for i in selected_word_list:
    hidden_list.append('_')

while game_playing:
    print(hidden_list)
    guess = input('What letter do you want to guess?')
    if guess in selected_word_list:
        print('Nice guess!')
    else:
        print('Nope!')
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/49240
 
1003 次点击  
文章 [ 3 ]  |  最新文章 4 年前