avp26/u8/program.py

83 lines
1.8 KiB
Python

import sys
class Node:
def __init__(self, text):
self.text = text
self.yes = None
self.no = None
def create_tree(lines, index, counts):
if index[0] >= len(lines):
raise ValueError
node = Node(lines[index[0]])
index[0] += 1
if node.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.")
lines = []
while True:
line = sys.stdin.readline()
if line == "":
print("Chybna databaza")
return
line = line.rstrip("\r\n")
if line == "":
break
lines.append(line)
if len(lines) == 0:
print("Chybna databaza")
return
counts = [0, 0]
index = [0]
try:
root = create_tree(lines, index, counts)
if index[0] != len(lines):
raise ValueError
except (ValueError, RecursionError):
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.")
current = root
while True:
print(current.text)
if not current.text.endswith("?"):
break
answer = sys.stdin.readline()
if answer == "":
print("Koniec vstupu")
return
answer = answer.rstrip("\r\n")
if answer == "a":
current = current.yes
elif answer == "n":
current = current.no
else:
print("Nerozumiem")
return
print("Koniec")
if __name__ == "__main__":
main()