随机化项目和循环列表(Python)



各位,我正在努力减轻我的一些工作,从面对面的教学环境迁移到远程教学环境,至少在接下来的三个月里。为此,我试图创建一个随机列表,列出符合条件的词汇,用于测验。我可以让它为相同数量的测验生成多达18组单词,但我似乎在笨拙地检查随机选择的单词是否已经是测验的一部分,即我得到了大量的重复。由于每天教一个单词,这意味着第一次每两周一次的测验将有十个符合条件的单词;第二,二十个字;第三,三十字;等等。请参阅下面的代码,并提前感谢您的帮助!

import random
all_words = "salutation feasible obnoxious depict resolve surmise assumption kindle intimidate jurisdiction allege characterize similar novice perceive condemn avid concise quench abrasive inevitable universal exonerate endeavor competent bamboozle simulate jeopardize prospective mutiny ubiquity integrate prevalent audacious reverberate corroborate feasible theory devour calamity boisterous amiss bizarre punctual simultaneous proprietor component famished significant conclude abdicate boycott consult rebuff eccentric imperative confront naive tirade ludicrous conscientious inhabitant confiscate admonish acknowledge embark prominent evoke libel modify imminent affiliation dawdle encompass indifferent perspective bias vitality necessity connotation despondent grieve gruesome perish devious sovereign propaganda construct prudent viewpoint liaison negligent relinquish mitigate formidable chronological commence ambiguous suspense consistent temperament impel assimilate gracious estimate apathy resilient opposition elapse killjoy obsolete signify satire hypothesis tumult harmonious fictitious authentic anonymous attribute procedure conclusive thrive capable interrogate legendary spontaneous addict merge evidence mandatory demeanor rebel despicable belligerent agitate oppress rebuke anarchy recur consecutive derogatory anthology valor elegant transit acquire authority contrast tentative profound specific exposition conjecture exuberant diversity abhor irate haughty irrelevant cause hospitable quaint inspire derive impartial industrious pseudonym omit prediction source apprehend bisect focus antagonize adequate bewildered correspond precise eligible".split(" ")
for week in range(1, 2):
print("---------------------")
print("Quiz #" + str(week))
print("---------------------")
print("WordttID")
print("---------------------")
for word in range(1, 10+1):
random_words = []

random_word = all_words[random.randrange(0, (week*10))]

# checks to see if random word is already
# part of the words on the upcoming quiz
if random_word not in random_words:
random_words.append(random_word)
# test to see if any of the ids match
# and that they fall within the eligible
# range for vocab words already taught
for test in random_words:
print('{:15} {}'.format(test, all_words.index(test)+1))
print("---------------------")

您可能想尝试使用random.sample(all_words, 10)并循环以扩展输出,以便在接下来的几周内使用(将10更改为所需的字数(。您需要先import random

最新更新