Доделал главу 7

This commit is contained in:
Марычев Павел Иванович 2021-07-31 20:53:55 +04:00
parent 8eaa630f67
commit dab0cfc73c
3 changed files with 136 additions and 0 deletions

93
ch7/analyze.py Normal file
View File

@ -0,0 +1,93 @@
# import ch1text
def count_syllables_in_word(word):
count = 0
endings = '.,;!?:'
last_char = word[-1]
if last_char in endings:
processed_word = word[:-1]
else:
processed_word = word
if len(processed_word) <= 3:
return 1
if processed_word[-1] in 'eE':
processed_word = processed_word[:-1]
vowels = 'aeiouAEIOU'
prev_char_was_vowel = False
for char in processed_word:
if char in vowels:
if not prev_char_was_vowel:
count += 1
prev_char_was_vowel = True
else:
prev_char_was_vowel = False
if processed_word[-1] in 'yY':
count += 1
return count
def count_syllables(words):
count = 0
for word in words:
word_count = count_syllables_in_word(word)
count = count + word_count
return count
def count_sentences(text):
count = 0
for char in text:
if char in ('.', '?', '!'):
count += 1
return count
def output_results(score):
if score >= 90:
return 'Уровень 5-го класса'
elif score >= 80:
return 'Уровень 6-го класса'
elif score >= 70:
return 'Уровень 7-го класса'
elif score >= 60:
return 'Уровень 8-9-го класса'
elif score >= 50:
return 'Уровень 10-11-го класса'
elif score >= 30:
return 'Уровень студента вуза'
else:
return 'Уровень выпускнинка вуза'
def compute_readability(text):
total_words = 0
total_sentences = 0
total_syllables = 0
score = 0
words = text.split()
total_words = len(words)
total_sentences = count_sentences(text)
total_syllables = count_syllables(words)
score = (206.835 - 1.015 * (total_words / total_sentences) - 84.6 * (total_syllables / total_words))
score_result = output_results(score)
# print(words)
print(total_words, 'слов')
print(total_sentences, 'предложений')
print(total_syllables, 'слогов')
print(score, ' - удобочитаемость')
print(score_result)
# print(text)
if __name__ == '__main__':
import ch1text
print('Текст главый 1:')
compute_readability(ch1text.text)

BIN
ch7/pavement.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 588 KiB

43
ch7/race.py Normal file
View File

@ -0,0 +1,43 @@
import turtle
import random
turtles = list()
def setup():
global turtles
startline = -620
screen = turtle.Screen()
screen.setup(1290,720)
screen.bgpic('pavement.gif')
turtle_ycor = [-40, -20, 0, 20, 40]
turtle_color = ['blue', 'red', 'purple', 'brown', 'green']
for i in range(0, len(turtle_ycor)):
new_turtle = turtle.Turtle()
new_turtle.shape('turtle')
new_turtle.penup()
new_turtle.setpos(startline, turtle_ycor[i])
new_turtle.color(turtle_color[i])
new_turtle.pendown()
turtles.append(new_turtle)
def race():
global turtles
winner = False
finishline = 590
while not winner:
for current_turtle in turtles:
move = random.randint(0,2)
current_turtle.forward(move)
xcor = current_turtle.xcor()
if (xcor >= finishline):
winner = True
winner_color = current_turtle.color()
print('The winner is', winner_color[0])
setup()
race()
turtle.mainloop()