Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3.2
- # -*-coding:Utf-8 -*
- #Vocabulary/translation quiz
- import os
- import random
- keep_adding=input("Would you like to add a new word ? If yes, press "O" : ")
- while keep_adding=="O":
- entry=[]
- word=input("Enter a word : ")
- word=str(word)
- entry.append(word)
- translation=input("And its translation : ")
- translation=str(translation)
- entry.append(translation)
- entry=str(entry)
- f = open("data.txt","a")
- f.write(entry)
- f.close()
- keep_adding=input("To continue, press "O" : ")
- f = open("data.txt","a") #in case the file doesn't exist, we create one
- f.close()
- os.system('clear')
- print("* * * QUIZ STARTS ! * * *")
- f = open("data.txt","r")
- text = f.readlines()
- text = list(text)
- print("What is the translation for : ",text[0], "?")
- answer = input("Answer : ")
- if (answer == text[1]):
- print("Congratulations ! That's the good answer !")
- else:
- print("Wrong. The correct answer was : ",text[1])
- * * * QUIZ STARTS ! * * *
- What is the translation for : ['alpha', 'bravo']['one', 'two']['x', 'y'] ?
- Answer : alpha
- Traceback (most recent call last):
- File "Python_progs/voc.py", line 43, in <module>
- if (answer == text[1]):
- IndexError: list index out of range
- ['alpha', 'bravo']['one', 'two']['x', 'y']
- f.write("line1")
- f.write("line2")
- f.write("line3")
- line1line2line3
- ["['alpha', 'bravo']['one', 'two']['x', 'y']"]
- [a_string,]
- if (answer == text[1]):
- quiz, answer
- alpha, bravo
- one, two
- etc...
- with open("myquizfile.txt", "w") as f:
- while keepGoing: #You'd have to add your own exit logic.
- question = input("Enter a question: ")
- answer = input("Enter an answer: ")
- f.write("{0},{1}n".format(question, answer)) #Notice the newline, n
- with open("myquizfile.txt", "r") as f:
- question_answer_pairs = [line.split(",") for line in f]
- #!/usr/bin/python3.2
- # -*-coding:Utf-8 -*
- #Vocabulary/translation quiz
- import os
- import random
- keep_adding=input("Would you like to add a new word ? If yes, press "O" : ")
- with open("data.txt","a") as f:
- while keep_adding=="O":
- word=input("Enter a word : ")
- translation=input("And its translation : ")
- f.write("{0},{1},n".format(word,translation))
- keep_adding=input("To continue, press "O" : ")
- #in case the file doesn't exist, we create one :
- with open("data.txt","a") as f:
- pass
- os.system('clear')
- print("* * * QUIZ STARTS ! * * *")
- with open("data.txt","r") as f:
- question = [line.split(",") for line in f]
- i = 0
- score = 0
- while i<len(question):
- num = random.randint(0,len(question)-1)
- print("nQuestion number ",i+1,": nWhat is the translation for ", question[num][0], "?")
- answer = input("Answer : ")
- if (answer == str(question[num][1])):
- print("Congratulations ! That's the good answer !")
- score += 1
- else:
- print("Wrong. The correct answer was : ",question[num][1])
- i += 1
- if len(question)==0:
- print("nThe file is empty. Please add new words to start the quiz !n")
- else:
- if i>1:
- qu_pl = "questions"
- else:
- qu_pl = "question"
- if score>1:
- sc_pl = "answers"
- else:
- sc_pl = "answer"
- print("n RESULTS :n ",i, qu_pl,", ",score,"correct ",sc_pl," n"
- ," --> Your score is : ",score*100/i,"% !n")
- ### WORDLIST ###
- list = ['castigate','brave','prowess','tenacious','pre-emptive','orthodox','arcane','corpuscle','putrid','infidel','stratagem','affinity','abnegate','infraction']
- mean = ['to scold','courageous','skill','stubborn','early','traditional','mysterious','a living cell','rotting','unbeliever','scheme','attraction','to renounce','violation of law']
- list1 = list.copy()
- mean1 = mean.copy()
- mean2 = mean.copy()
- mean3 = mean.copy()
- mean4 = mean.copy()
- from random import randint
- import random
- word_count = len(list) - 1
- options = []
- options = random.sample(range(1, word_count), 4)
- options1 = options.copy()
- options2 = options.copy()
- options3 = options.copy()
- options4 = options.copy()
- opt1 = options1.pop(0)
- opt2 = options2.pop(1)
- opt3 = options3.pop(2)
- opt4 = options4.pop(3)
- x = randint(0,3)
- y = options.pop(x)
- #ignore [options]
- question = list.pop(y)
- answer = mean.pop(y)
- print("What is the meaning of",question,"?")
- print("1)",mean1.pop(opt1))
- print("2)",mean2.pop(opt2))
- print("3)",mean3.pop(opt3))
- print("4)",mean4.pop(opt4))
- while True:
- try:
- userans = int(input("nYour answer: "))
- except ValueError:
- print("Sorry, I didn't get that.")
- continue
- else:
- break
- if userans - 1 is x:
- print("Your answer is correct!")
- else:
- if userans is 100:
- print("There are %s words in your personal dictionary" % word_count)
- else:
- if userans > 4:
- print("It's OK if you do not know the answer. Better luck next time:)nThe meaning of %s is %s!" % (question, answer))
- else:
- print("Your answer is wrong!nThe meaning of %s is %s!" % (question, answer))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement