Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/home/ounis/pyapps/bin/python
- # PASTEBIN: https://pastebin.com/35ueCmXf
- import random
- from warnings import warn
- from time import sleep
- import json
- import sys
- VOCS_DICT_KEY_NAME = "vocabulary"
- VOCS_DICT_VALUE_NAME = "answers"
- INDEX_VOC = 0
- INDEX_ANSW_LIST = 1
- INDEX_GOOD_ANSWER = 0
- TERMINATE_WORD = ("quit","matei")
- # lista słówek
- # schema:
- # (vocabulary, (good_answer, possible_answer1, possible_answer2, ...))
- lst_dict_vocs = ({VOCS_DICT_KEY_NAME : "ch_j", VOCS_DICT_VALUE_NAME: ("u", "ó")}, )
- vocabularies = [("w_zek",("ó","u", "R")),
- ("p_sty", ("u", "ó")),
- ("bo_ater", ("h", "ch")),
- ("r_bin", ("u", "ó")),
- ("wr_bel", ("ó", "u", "a")),
- ("w_dka", ("ó", "u")),
- ("d_pa", ("u", "ó")),
- ("ch_j", ("u", "ó")),
- ("k_rwa", ("u", "ó")),
- ("tchó_", ("rz", "ż")),
- ]
- # print(lst_dict_vocs)
- # exit(619)
- def convLstVocs2DictVocs(aListVocs):
- # ( "k_rwa", ("u", "ó") ) -> {"vocabulary" : "k_rwa", "answers": ("u", "ó")}
- d = {}
- vocabulary = aListVocs[0]
- answers = aListVocs[1]
- d[VOCS_DICT_KEY_NAME] = vocabulary
- d[VOCS_DICT_VALUE_NAME] = answers
- return d
- #
- # for v in vocabularies:
- # print(convLstVocs2DictVocs(v))
- # exit(616)
- def save2FileJSON(aFileName, aVocs):
- temp_d ={}
- result = ""
- for v in aVocs:
- temp_d = convLstVocs2DictVocs(v)
- json_temp = json.dumps(temp_d)
- result = result + json_temp
- # result += "}\n"
- with open(aFileName, "w") as f:
- f.write(result)
- # save2FileJSON("./quuuuizeeee.json", vocabularies)
- # exit(616)
- def save2File(aFileName, aVocs):
- temp = ""
- with open(aFileName, "w") as fw:
- for voc in aVocs:
- temp = f'{voc[INDEX_VOC]}={",".join([a for a in voc[INDEX_ANSW_LIST]])}'
- # print(temp)
- fw.write(temp+"\n")
- def loadFromFile(aFileName, aVocs):
- print("ładowanie pytań...")
- if len(aVocs) > 0:
- aVocs.clear()
- with open(aFileName) as fr:
- for line in fr:
- line = line.rstrip()
- lst = line.split("=")
- voc = lst[0]
- answs = lst[1]
- aVocs.append((voc, ))
- aVocs[-1] += (tuple((a for a in answs.split(","))), )
- print(getCorrectPhrase(aVocs[-1]))
- print("Załadowano:",len(aVocs))
- return aVocs
- def getRandomAnswList(anAnsw):
- # warn("już nie używane...s")
- return random.sample(anAnsw, len(anAnsw))
- def getPhrase(aVoc, valIndex):
- voc = aVoc[0]
- corr_v = aVoc[1][valIndex]
- phrase = ""
- for c in voc:
- if c != "_":
- phrase = phrase + c
- else:
- phrase = phrase + "".join([x for x in corr_v])
- return phrase
- def getCorrectPhrase(aVoc):
- return getPhrase(aVoc, INDEX_GOOD_ANSWER)
- def getIncorrectPhrase(aVoc):
- return getPhrase(aVoc, INDEX_GOOD_ANSWER + 1)
- # for v in vocabularies:
- # print(getCorrectPhrase(v))
- # exit()
- # generate random qustion list
- def getRandomizedQuestionsIndexList(aScope):
- warn("JUŻ NIE UŻYWANE...")
- res = ()
- while len(res) != aScope:
- q = random.randrange(aScope)
- if not q in res:
- res += (q,)
- # print([q+1 for q in res])
- return res
- # exit()
- def beep(aCount):
- for _ in range(aCount):
- print("\a", flush=True, end="")
- sleep(0.2)
- def genFailComment(anId=-1):
- return random.choice(("No ty chyba śnisz...",
- "NIE!!! Odpowiedź błędna!!!",
- "Ale słabo...!!!",
- "No i żle!!! Mundrusiu ty...!!!",
- "Żenuła...!!! "))
- def main(args):
- print(args[0])
- # save2File("quuuuizeeee.dat", vocabularies)
- # vocabularies.clear()
- global vocabularies
- vocabularies = loadFromFile("quuuuizeeee.dat", vocabularies)
- # exit(619)
- SCORE = len(vocabularies)
- MAX = SCORE
- TERMINATE_BY_USER = False
- for v in random.sample(vocabularies, len(vocabularies)):
- # v -> (voc, (answ1, answ2, answn...))
- # if cnt > question_count:
- # break
- print(f"\n\t\t{v[INDEX_VOC]}")
- good_a = v[INDEX_ANSW_LIST][INDEX_GOOD_ANSWER]
- print("Wybierz brakującą literę: ")
- ran_answ = getRandomAnswList(v[INDEX_ANSW_LIST])
- # print("ran_answ", ran_answ, "answ", v[1])
- # ran_answ = random.sample(v[1], len(v[1]))
- for i, a in enumerate(ran_answ):
- print(f'\t {i+1} - {a.upper()}')
- # passed = False
- while True:
- try:
- temp = input("\t? ");
- if temp.lower() in TERMINATE_WORD:
- TERMINATE_BY_USER = True
- break
- user_answ = int(temp)
- if not (user_answ > 0 and user_answ < len(ran_answ)+1 ):
- raise IndexError
- except:
- print(f"zły wybór, spróbuj wartości między 1 a {len(ran_answ)}")
- continue
- else:
- break
- # passed = True
- # print(good_a, ran_answ[user_answ-1], user_answ,"->", user_answ-1)
- if TERMINATE_BY_USER:
- print("Przerwane przez użyszkodnika...")
- break
- if ran_answ[user_answ-1] == good_a:
- print("Zgadza się!!!")
- else:
- print(f'{genFailComment()}')
- beep(2)
- SCORE -= 1
- # cnt += 1
- else:
- print("\n\nMożliwych punktów do zdobycia:", MAX)
- print("WYNIK:", SCORE)
- # print(vocabularies)
- if __name__ == "__main__":
- sys.exit(main(sys.argv))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement