当教完Python列表和字典后,首先编写了两个英语单词记忆小程序,用于让学生熟练列表和字典的使用方法:
第一个程序如下所示 :
import random e = ['append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort','dictionary','tuple','set'] c = ['添加','清空','复制' ,'统计', '扩展','索引', '插入','弹出','删除','反转','排序','字典','元组','集合'] a = 0 while a>=0: i = random.randint(0,len(e)-1) print(e[i]) for k in range(len(c)): print(k,c[k],end=' ') print() while a>=0: j = int(input()) if j==i: print('正确,加10分') a = a +10 break else: print('错误,-10,请重试') a = a - 10 print() print("失败!")
第二个程序如下所示:
Python简单的英语单词记忆小程序 import random e = ['append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort','dictionary','tuple','set'] c = ['添加','清空','复制' ,'统计', '扩展','索引', '插入','弹出','删除','反转','排序','字典','元组','集合'] a = 0 d = dict(zip(e,c)) while a>=0: word = random.choice(e) print(word) options = [d[word]] # 继续加三个翻译,但不能和正确的重复 while len(options)!=4: w = random.choice(c) # 随机选择一个中文 if w!=d[word] and w not in options: options.append(w) random.shuffle(options) #洗牌(打乱顺序) for k in range(len(options)): # 输出4个选项 print(k,options[k],end=' ') print() while a>=0: j = int(input()) if options[j] == d[word]: print("正确,加10分") a += 10 break else: print('错误,减10分') a -=10 print()