Skip to content

Instantly share code, notes, and snippets.

@mewforest
Created April 26, 2023 18:35
Show Gist options
  • Save mewforest/c4dd79975ce372d95c9a67ae25d66dc1 to your computer and use it in GitHub Desktop.
Save mewforest/c4dd79975ce372d95c9a67ae25d66dc1 to your computer and use it in GitHub Desktop.
1. Симулятор светофора, нужно реализовать программу, которая будет выводить в консоль свет светофора, а в ввод получать действие пешехода, если он не успевает перейти - его сбивает, если он идёт не на тот свет его сбивает Здесь нужно будет использовать циклы, условия и библиотеку time
from datetime import datetime
TURN_TIME = 3
TRAFFIC_LIGHTS = (
'🔴',
'🟡',
'🟢',
)
USER_ACTIONS = (
'🧍СТОИШЬ',
'🙋‍ГОТОВИШЬСЯ',
'🚶ИДЕШЬ',
)
def play():
score = 0
light_index = 0
while True:
light_str = TRAFFIC_LIGHTS[light_index]
print('* ' * 10)
text_tip = '\nℹ️ Подсказка для игры:\n'
for i, action in enumerate(USER_ACTIONS):
text_tip += f'\t[{i}] - {action}\n'
print(text_tip.rstrip(), end="\n\n")
start_dt = datetime.now()
input_number = input(f'Светофор горит {light_str}, ты .. > ')
user_input_dt = datetime.now()
if not input_number.isdigit():
print('Ошибка! Можно вводить только числа')
return score
action_index = int(input_number)
if action_index < 0 or action_index > len(USER_ACTIONS) - 1:
print('Ошибка! Нет такого действия, внимательно читай подсказку')
return score
if light_index != action_index:
print(f'Ошибка! Ты выбрал {USER_ACTIONS[action_index]}, но когда {light_str} нужно {USER_ACTIONS[light_index]}')
return score
current_turn_time = (user_input_dt - start_dt).seconds
if current_turn_time > TURN_TIME:
print(f'Ты не успел! Нужно было управиться за {TURN_TIME}сек, а ты сделал это за {round(current_turn_time, 2)}сек')
return score
print('Успех! +10 очков гриффиндору!\n')
score += 10
light_index += 1
if light_index >= len(TRAFFIC_LIGHTS):
light_index = 0
if __name__ == '__main__':
print(f'▶️ Нажми Enter, чтобы начать игру в светофор. (скорость хода: {TURN_TIME}с)')
input()
total_score = play()
print(f'\n***\n\n🏁 Game over! Ты заработал {total_score} очков!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment