【发布时间】:2016-04-09 12:01:55
【问题描述】:
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 数据结构的文档docs.python.org/3.1/tutorial/datastructures.html
-
我是 python 新手。你能告诉它是如何实现的吗??
-
只需使用
my_list_variable[item_index]。
标签: list python-3.x indexing