List 在python中如何通过索引选择列表中的单词
在代码中,用户直接输入猜测词。是否有任何方法可以让用户输入猜测作为单词索引,而不是完全键入完整的单词。尝试此方法希望您正在尝试此方法:List 在python中如何通过索引选择列表中的单词,list,python-3.x,indexing,List,Python 3.x,Indexing,在代码中,用户直接输入猜测词。是否有任何方法可以让用户输入猜测作为单词索引,而不是完全键入完整的单词。尝试此方法希望您正在尝试此方法: import random print('Welcome to the Guess-The-Word Game.') print('Password is one of the words:') wordList = ['AETHER', 'BADGED', 'BALDER', 'BANDED', 'BANTER', 'BARBER', 'BASHER', '
import random
print('Welcome to the Guess-The-Word Game.')
print('Password is one of the words:')
wordList = ['AETHER', 'BADGED', 'BALDER', 'BANDED', 'BANTER', 'BARBER', 'BASHER', 'BATHED']
for index,word in enumerate(wordList):
print(index , '-', word)
password =(random.choice(wordList))
guess = None
counter = 0
while guess != password:
guess = input('enter your guess:')
counter = counter + 1
if counter ==4:
print('Game over')
elif guess == password:
print(password, ' is correct')
else:
print('wrong guess try again')
是的,有。你有什么真正的问题吗?这里是关于Python 3.x数据结构的文档,我是Python新手。您能告诉我如何实现吗?只需使用
my\u list\u变量[item\u index]
。
import random
print('Welcome to the Guess-The-Word Game.')
print('Password is one of the words:')
wordList = ['AETHER', 'BADGED', 'BALDER', 'BANDED', 'BANTER', 'BARBER', 'BASHER', 'BATHED']
status_flag = False
guess = None
counter = 0
password =(random.choice(wordList))
word_index = wordList.index(password)
max_index = len(wordList)-1
while guess != password:
guess = input('Enter your guess:[0 -'+str(max_index)+']')
if guess > max_index:
print ('Wrong input: Enter your guess:[0 -'+str(max_index)+']')
continue
counter = counter + 1
if counter ==4:
print('Game over')
status_flag = True
elif guess == word_index:
print(str(password)+' is correct')
status_flag = True
else:
print('Wrong guess try again')
if status_flag:
break