Aktualizovat u8/program.py

This commit is contained in:
Oleksandr Mateshchuk 2026-11-03 13:37:04 +00:00
parent ac333aaa6f
commit 727452449e

View File

@ -1,3 +1,4 @@
import sys
class Node: class Node:
def __init__(self, text): def __init__(self, text):
@ -5,8 +6,9 @@ class Node:
self.yes = None self.yes = None
self.no = None self.no = None
def create_tree(lines, index, counts): def create_tree(lines, index, counts):
if index[0] >= len(lines):
raise ValueError
node = Node(lines[index[0]]) node = Node(lines[index[0]])
index[0] += 1 index[0] += 1
@ -22,35 +24,29 @@ def create_tree(lines, index, counts):
def main(): def main():
print("Expert zo zoo to vie.") print("Expert zo zoo to vie.")
lines = [] lines = []
while True: while True:
try: line = sys.stdin.readline()
line = input() if line == "":
except EOFError:
print("Chybna databaza") print("Chybna databaza")
return return
line = line.rstrip("\r\n")
if line == "": if line == "":
break break
lines.append(line) lines.append(line)
if len(lines) == 0: if len(lines) == 0:
print("Chybna databaza") print("Chybna databaza")
return return
counts = [0, 0] counts = [0, 0]
index = [0] index = [0]
try: try:
root = create_tree(lines, index, counts) root = create_tree(lines, index, counts)
except (IndexError, RecursionError): if index[0] != len(lines):
print("Chybna databaza") raise ValueError
return
if index[0] != len(lines): except (ValueError, RecursionError):
print("Chybna databaza") print("Chybna databaza")
return return
@ -62,16 +58,15 @@ def main():
while True: while True:
print(current.text) print(current.text)
if not current.text.endswith("?"): if not current.text.endswith("?"):
break break
try: answer = sys.stdin.readline()
answer = input() if answer == "":
except EOFError:
print("Koniec vstupu") print("Koniec vstupu")
return return
answer = answer.rstrip("\r\n")
if answer == "a": if answer == "a":
current = current.yes current = current.yes
elif answer == "n": elif answer == "n":