A console quiz with Python

A console quiz made with Python that gets the questions from an external txt file.

# pyquiz 0.1 # 00 # 01
from random import shuffle
import sys, time
import os

class Dic:
	def __init__(self, filename):
		self.filename = filename
		self.list_from_file()
		self.dic_from_list()

	def list_from_file(self):
		"Create a list from the file with data"
		self.list_of_questions = []
		text = ""
		with open(self.filename) as file:
			for line in file:
				text += line.strip()
				if line == "\n":
					self.list_of_questions.append(text[:-1])
					text = ""
				else:
					text += "#"
			self.list_of_questions.append(text[:-1])
	
	def dic_from_list(self):
		"Create a dictionary from self.questions"
		self.dict_of_questions = {}
		counter = 0
		for q in self.list_of_questions:
			counter += 1
			qna = q.split("#")
			self.dict_of_questions[f"question{counter}"] = qna
	
	def print_at_console(self):
		"Print at console"
		for x in self.dict_of_questions:
			print("=====================")
			self.texttime(self.dict_of_questions[x][0])
			print("=====================")
			rnd_ans = self.dict_of_questions[x][1:]
			shuffle(rnd_ans)
			for qna in rnd_ans:
				print(qna)
			print("---------\n")
			ans = input("What you choose? ")
			print("\n"*10)
			if ans == self.dict_of_questions[x][1]:
				self.texttime("> You are right\n")
			else:
				self.texttime("> No, your are wrong\n")
			input("Hit enter")
			print("\n"*10)

	def texttime(self, words):
		for c in words:
			sys.stdout.write(c)
			sys.stdout.flush()
			time.sleep(0.1)

	def shuffle(self):
		shuffle(self.list_of_questions)
		self.dic_from_list()


questions1 = Dic("questions\\tourism.txt")
questions1.shuffle()
questions1.print_at_console()


New version (in italian)

Some changes. You can now choose with the number of the question.

# pyquiz 0.1 # 00 # 01
from random import shuffle, choice
import sys, time
import os

class Dic:
	def __init__(self, filename):
		self.filename = filename
		self.list_from_file()
		self.dic_from_list()

	def list_from_file(self):
		"Create a list from the file with data"
		self.list_of_questions = []
		text = ""
		with open(self.filename) as file:
			for line in file:
				text += line.strip()
				if line == "\n":
					self.list_of_questions.append(text[:-1])
					text = ""
				else:
					text += "#"
			self.list_of_questions.append(text[:-1])
	
	def dic_from_list(self):
		"Create a dictionary from self.questions"
		self.dict_of_questions = {}
		counter = 0
		for q in self.list_of_questions:
			counter += 1
			qna = q.split("#")
			self.dict_of_questions[f"question{counter}"] = qna
	
	def print_at_console(self):
		"Print at console"
		for x in self.dict_of_questions:
			self.texttime("=====================")
			print(self.dict_of_questions[x][0])
			rnd_ans = self.dict_of_questions[x][1:]
			shuffle(rnd_ans)
			for n, qna in enumerate(rnd_ans):
				self.texttime(f"{n+1}. {qna}")
			self.texttime("---------")
			ans = input("Cosa scegli? ")
			print(f"hai risposto [{rnd_ans[int(ans)-1]}]\n")
			if rnd_ans[int(ans)-1] == self.dict_of_questions[x][1]:
				risposta = self.rispostacasuale(r="giusta")
				self.texttime(f"> {risposta}\n")
			else:
				risposta = self.rispostacasuale(r="sbagliata")
				self.texttime(f"> {risposta}\n")
			# self.texttime(". . . . . . . ."*2)
			self.texttime("\n"*3)
			print(x)

	def rispostacasuale(self, r="giusta"):
		if r == "giusta":
			g = choice([
				"Bravo/a",
				"Esatto",
				"Complimenti",
				"Benissimo"
				])
			return g
		else:
			g = choice([
				"Non è così, mi dispiace.",
				"Non è così",
				"Sbagliato"
				])
			return g

	def texttime(self, words):
		for c in words:
			sys.stdout.write(c)
			sys.stdout.flush()
			time.sleep(0.02)
		print()

	def shuffle(self):
		shuffle(self.list_of_questions)
		self.dic_from_list()

listofquestions = os.listdir("questions")
[print(n, x) for n, x in enumerate(listofquestions)]
print()
num = input("File da caricare > ")
print()
questions1 = Dic(f"questions/{listofquestions[int(num)]}")
questions1.shuffle()
questions1.print_at_console()

 


Subscribe to the newsletter for updates
Tkinter templates
My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts