在我的.csv文件中,我总共有12个国家(有12个首都)我想随机选择这12个国家中的10个。
我从stackoverflow资源中找到了一些单一或一对选择,但没有从字典中随机选择10个项目。我怎么能这样做?
这是我用于国家和首都考试的相关代码,用户将资金输入所询问的国家/地区,并输出正确或错误的答案。
#Defining the function of reading .csv file from path into dictionary
def readCsvIntoDictionary(path):
dic = {}
with open(path, 'r', newline='') as f:
reader = csv.reader(f)
for row in reader:
dic[row[0]] = row[1];
return dic;
count = 0;
# for loop with key variable definition in dic
for key in dic:
# ans variable defined for user input
ans = input('What is the capital of ' + key + '?\n')
# User can input lower and upper answers
if(ans.lower() == dic[key].lower()):
# key lookup in dic (dictionary) if answer is correct at end of the loop
dic[key] = 'Correct! ' + dic[key] + ' is the capital of ' + key;
count = count + 1;
else:
# key lookup in dic (dictionary) if answer is incorrect at end of the loop
dic[key] = 'Wrong! \'' + ans + '\' is not the capital of ' + key + ', it\'s ' + dic[key];
谢谢!