81 lines
1.6 KiB
Python
81 lines
1.6 KiB
Python
class Node:
|
|
def __init__(self, text):
|
|
self.text = text
|
|
self.yes = None
|
|
self.no = None
|
|
|
|
|
|
def create_tree(lines, index, counts):
|
|
text = lines[index[0]]
|
|
index[0] += 1
|
|
|
|
node = Node(text)
|
|
|
|
if text.endswith("?"):
|
|
counts[0] += 1
|
|
node.yes = create_tree(lines, index, counts)
|
|
node.no = create_tree(lines, index, counts)
|
|
else:
|
|
counts[1] += 1
|
|
|
|
return node
|
|
|
|
|
|
def main():
|
|
print("Expert zo zoo to vie.")
|
|
print()
|
|
lines = []
|
|
|
|
while True:
|
|
line = input()
|
|
if line == "":
|
|
break
|
|
lines.append(line)
|
|
|
|
if not lines:
|
|
print("Chybna databaza")
|
|
return
|
|
|
|
counts = [0, 0]
|
|
index = [0]
|
|
|
|
try:
|
|
root = create_tree(lines, index, counts)
|
|
if index[0] != len(lines):
|
|
print("Chybna databaza")
|
|
return
|
|
|
|
except (IndexError, EOFError):
|
|
print("Chybna databaza")
|
|
return
|
|
|
|
print(f"Pocet otazok: {counts[0]}")
|
|
print(f"Pocet odpovedi: {counts[1]}")
|
|
print("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.")
|
|
print()
|
|
|
|
current = root
|
|
|
|
while current.text.endswith("?"):
|
|
print(current.text)
|
|
try:
|
|
answer = input()
|
|
except EOFError:
|
|
print("Koniec vstupu")
|
|
return
|
|
|
|
if answer == "a":
|
|
current = current.yes
|
|
elif answer == "n":
|
|
current = current.no
|
|
else:
|
|
print("Nerozumiem")
|
|
return
|
|
|
|
print(current.text)
|
|
print("Koniec")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|