When we were studying for exams, Tobias and Peder1 decided to make some tools to help us study. This is Tobias’ version, written in Python.
“The program is fairly simple. You select a text file containing lines with topics you want to learn, and then you have to type inn all of them. The program writes how many topics are left, and can also give you hints if you need it. You are finished when you have written all topics, or have given up by typing “stop”. Then it writes out how long time you used.”
Read more for the code
import time
import os
avail_topics_in_file = []
topics = []
#Traverse folder
for root, dirs, files in os.walk("."):
for file in files:
#Check if file ends with .txt
if file.endswith(".txt"):
avail_topics_in_file.append(file)
#print topics
print "Select topic:"
for i in range(0,len(avail_topics_in_file)):
print i+1,avail_topics_in_file[i]
#find topic
selected_topic = int(raw_input())-1
chosen_file = avail_topics_in_file[selected_topic]
print "You have selected",chosen_file.split(".")[0] + "."
#Run through file and add each line to topics
f = open(chosen_file)
for line in f:
topics.append(line.strip())
#Save answer to answer-variable
answer = topics
topics_len = len(topics)
#Initialize array and number of found topics.
found_topics = []
found_topics_len = 0
#Save number of hints used, and how many character we show for each hint
used_hints = 0
helpchars = 2
start = time.time()
#Function which print strint with the title function
def print_title(x):
print x.title()
#Function which prints all entries in the list.
def print_all_from_list(list):
map(lambda x: print_title(x),list)
print "To recieve a hint, please type \"hint\"."
#Loop which runs until all topics are found or user breaks it by typing "stop"
while True:
print "Here are your currently solved words:"
#Check how many hints are left
if 5-used_hints < 0:
hint_text = 0
else:
hint_text = 5 - used_hints
print "You have " + str(hint_text) + " hint igjen."
#Print all found topics
for i in found_topics:
print "-" ,i
#print number of found topics / all topics.
print found_topics_len , "of" , topics_len
#Get new input
input = raw_input().lower()
#check if input is one of the answers, and remove it if it is.
if input in topics:
for i in range(0, topics_len):
if input == topics[i-1]:
topics.pop(i-1)
break
#Increase number of found topics, and add it to the found_topics-list.
found_topics_len += 1
found_topics.append(input)
#Reset the helpindex
helpchars = 2
#Check if all topics are found and print some information
if found_topics_len == topics_len:
print "\nHurray! You won the game!"
time_used = time.time() - start
print "You needed " + str(used_hints) + " hints and used" ,
str('%.2f'%(time_used)) + " seconds in total.\n"
print "Here are the answers:"
print_all_from_list(found_topics)
break
#user wants a hint.
elif input == "hint":
if(used_hints < 5):
print "\nHint:" , topics[0][0:helpchars]
else:
print "Du er tom for hint!"
helpchars += +2
used_hints += 1
#user wants to stop.
elif input == "stop":
print "You gave up. Poor you. Here's the answers:"
print_all_from_list(answer)
break
else:
#Word is not found.
print "Word not found. Sorry"
print "\n"
Related posts: