.
This commit is contained in:
58
bac1/q1/13oct/userInput.py
Normal file
58
bac1/q1/13oct/userInput.py
Normal file
@ -0,0 +1,58 @@
|
||||
def belongs_to_dictionary(word):
|
||||
"""check if a word is in dictionary
|
||||
:word: the word to check
|
||||
:returns: true if in word.txt else false
|
||||
"""
|
||||
with open("words.txt") as wl:
|
||||
word_list = wl.read().split()
|
||||
return word in word_list
|
||||
|
||||
|
||||
def ask_word_in_dictionary():
|
||||
""" ask a word, if not in word.txt redo
|
||||
:returns: word of word.txt
|
||||
"""
|
||||
while(True):
|
||||
in_word = input("insert a word that belongs to the file words.txt: ")
|
||||
if belongs_to_dictionary(in_word):
|
||||
return in_word
|
||||
print(f"{in_word} is not in word.txt")
|
||||
|
||||
|
||||
def ask_letter(tried_letters):
|
||||
""" ask for a letter and check if it is not in trie_letters else redo
|
||||
:tried_letters: list of already tried letters
|
||||
:returns: the letter that is not in tried letters
|
||||
"""
|
||||
while(True):
|
||||
in_letter = input(f"insert a letter that is not tested yet: {tried_letters.split()}: ")
|
||||
if in_letter not in tried_letters:
|
||||
return in_letter
|
||||
print(f"{in_letter} is already guessed")
|
||||
|
||||
|
||||
if __name__ == "__main__": # Only tests
|
||||
print("Test de la fonction, belongs_to_dictionary")
|
||||
testing_words = {'banana': True,
|
||||
'patatas': False,
|
||||
'tonitch': False,
|
||||
'other': True,
|
||||
'lol': False,
|
||||
1: False,
|
||||
False: False}
|
||||
for value, result in testing_words.items():
|
||||
if belongs_to_dictionary(value) == result:
|
||||
print(f"{value} has been tested, returned {result} -- SUCCESS!")
|
||||
else:
|
||||
print(f"{value} has been tested, returned {result} -- ERROR!")
|
||||
|
||||
print("Test de la fonction, ask_word_in_dictionary")
|
||||
value = ask_word_in_dictionary()
|
||||
print(f"The function returned value {value}")
|
||||
|
||||
print("Test de la fonction, ask_letter")
|
||||
value = ask_letter("")
|
||||
print(f"The function returned value {value}")
|
||||
value = ask_letter("aeiou")
|
||||
print(f"The function returned value {value}")
|
||||
print("Test are finished")
|
Reference in New Issue
Block a user