Python 3.5“for循环”“enumerate()”从列表中选择单词
嗨,我是新来的我刚开始学习python 单词表中的单个单词将作为变量称为“单词” 顺便问一下,我会为python英语语法gen程序设置这个 我还想尝试输入一个循环,每个单词在单词列表中重复一次。尝试使用带有“枚举”功能的for循环,但我一直被卡住。任何帮助都会很好Python 3.5“for循环”“enumerate()”从列表中选择单词,python,python-3.x,for-loop,enumerate,Python,Python 3.x,For Loop,Enumerate,嗨,我是新来的我刚开始学习python 单词表中的单个单词将作为变量称为“单词” 顺便问一下,我会为python英语语法gen程序设置这个 我还想尝试输入一个循环,每个单词在单词列表中重复一次。尝试使用带有“枚举”功能的for循环,但我一直被卡住。任何帮助都会很好 print('Welcome to my English Test App') # Import the random module to allow us to select the word list and questio
print('Welcome to my English Test App')
# Import the random module to allow us to select the word list and questions at random.
import random
candidateWords = ['HELLO', 'GOODBYE', 'NAME', 'DAY', 'NIGHT', 'HOUR', 'POTATO', 'BIG', 'SMALL', 'GOOD', 'BAD', 'YES', 'NO', 'HOUSE', 'QUESTION', 'BALLOON', 'CAT', 'DUCK', 'PIGEON', 'POSTER', 'TELEVISION', 'SPY', 'RHYTHM', 'SUBSTANTIAL', 'SNOW', 'MAGNET', 'TOWEL', 'WALKING', 'SPEAKER', 'UNCHARACTERISTICALLY']
# places words down in a list
s = random.sample (candidateWords, 5)
for index,w in enumerate(s):
print(index,w)
如果您的问题是如何使用enumerate,下面是代码示例的应用程序:
import random
candidateWords = ['HELLO', 'GOODBYE', 'NAME', 'DAY', 'NIGHT', 'HOUR', 'POTATO', 'BIG', 'SMALL', 'GOOD', 'BAD', 'YES', 'NO', 'HOUSE', 'QUESTION', 'BALLOON', 'CAT', 'DUCK', 'PIGEON', 'POSTER', 'TELEVISION', 'SPY', 'RHYTHM', 'SUBSTANTIAL', 'SNOW', 'MAGNET', 'TOWEL', 'WALKING', 'SPEAKER', 'UNCHARACTERISTICALLY']
s = random.sample (candidateWords, 5)
# loop through the list using enumerate
for index,w in enumerate(s):
print("word {} out of {} is {}, nb vowels is {}".format(index+1,len(s),w,countVowels(w)))
输出
word 1 out of 5 is NIGHT, nb vowels is 1
word 2 out of 5 is BALLOON, nb vowels is 3
word 3 out of 5 is SUBSTANTIAL, nb vowels is 4
word 4 out of 5 is BIG, nb vowels is 1
word 5 out of 5 is PIGEON, nb vowels is 3
请注意,如果不需要索引,则不必使用enumerate:
for w in s:
print(w,countVowels(w))
对于你的元音计数,因为每个人似乎都认为在某个时候这是一个问题,你可以这样做,Y是否是元音取决于你,因为它不清楚
生成器comp将为单词中的每个元音应用计数字符,然后应用标准求和函数。实际上您的问题不清楚,您的预期输出是什么。?我目前正在尝试从列表中获取5个单词。尝试将单词列表中的单个单词称为“单词”。要使用emutrate循环显示单词question1/5:,Question2/5:ECT下一行随机。候选单词示例,5将给出正确答案?是的,这只是减少列表中的单词数量,因为我们现在只需要5个,然后我们需要使用enumerate循环将这些单词放入列表中,您的co中既没有循环也没有enumeratede?和OP不是在寻找这个。你是对的,修复并添加了Count元音解决方案作为奖励,因为OP可能会在下一个将来问这个问题。有没有办法将它显示为单词1/5:??但谢谢这为我澄清了一点:谢谢这么多的朋友,你们帮了你们所有人的大忙。经过所有的努力,如果有效,请接受a回答:
print('Welcome to my English Test App')
#Import the random module to allow us to select the word list and questions at random.
import random
candidateWords = ['HELLO', 'GOODBYE', 'NAME', 'DAY', 'NIGHT', 'HOUR', 'POTATO', 'BIG', 'SMALL', 'GOOD', 'BAD', 'YES', 'NO', 'HOUSE', 'QUESTION', 'BALLOON', 'CAT', 'DUCK', 'PIGEON', 'POSTER', 'TELEVISION', 'SPY', 'RHYTHM', 'SUBSTANTIAL', 'SNOW', 'MAGNET', 'TOWEL', 'WALKING', 'SPEAKER', 'UNCHARACTERISTICALLY']
# places words down in a list
s = random.sample (candidateWords, 5)
for index,w in enumerate(s):
print(index,w) #your code have indent problem
print('Welcome to my English Test App')
#Import the random module to allow us to select the word list and questions at random.
import random
candidateWords = ['HELLO', 'GOODBYE', 'NAME', 'DAY', 'NIGHT', 'HOUR', 'POTATO', 'BIG', 'SMALL', 'GOOD', 'BAD', 'YES', 'NO', 'HOUSE', 'QUESTION', 'BALLOON', 'CAT', 'DUCK', 'PIGEON', 'POSTER', 'TELEVISION', 'SPY', 'RHYTHM', 'SUBSTANTIAL', 'SNOW', 'MAGNET', 'TOWEL', 'WALKING', 'SPEAKER', 'UNCHARACTERISTICALLY']
# places words down in a list
s = random.sample (candidateWords, 5)
for index,w in enumerate(s):
print(index,w) #your code have indent problem