import random
from threading import Timer
timeout = 20
count = 0
print('отделять НЕ запятой, А точкой')
while True:
num = 0
num_one = random.randint(1, 50)
num_two = random.randint(1, 50)
operation = random.randint(1, 3)
if operation == 1:
operation = '+'
num = num_one + num_two
elif operation == 2:
operation = '-'
num = num_one - num_two
elif operation == 3:
operation = '*'
num = num_one * num_two
print(num_one, operation, num_two, end=' = ')
t = Timer(timeout, print, ['\nВРЕМЯ ВЫШЛО!'])
t.start()
user = int(input())
t.cancel()
if user == num:
print('\nВЕРНО!\n')
count += 10
timeout -= 0.01
elif user == 0:
break
else:
break
file = open("win.txt", "r")
best = file.read ()
if count > int(best):
file_open = open("win.txt", "w")
file_open.write(str(count))
file_open.close()
print(f'Увы, вы проиграли. Ваш рекорд: {count}')
print('Лучший рекорд:', best)
r = input()
if r == 'r':
re = open('win.txt', 'w')
re.write(str(0))
file.close()
хочу что бы по истечению 15 секунд выводилось ВРЕМЯ ВЫШЛО! и завершался while
Системное администрирование
Помогите с Python
Вместо
И добавь перед этим кодом импорты:
Это - питоновская обёртка над стандартным юниксовым select.
В Винде - только дрюкать в цикле msvcrt.kbhit() и спать между итерациями, пока либо не будет ввода, либо не сработает таймаут. Решение - под стать системе.
user = int(input())
поставь inp, _, _ = select([stdin], [], [], 15)
if inp:
user = int(input())
# логика обработки ввода
else:
# логика, срабатывающая по таймауту
И добавь перед этим кодом импорты:
from select import select
from sys import stdin
Это - питоновская обёртка над стандартным юниксовым select.
В Винде - только дрюкать в цикле msvcrt.kbhit() и спать между итерациями, пока либо не будет ввода, либо не сработает таймаут. Решение - под стать системе.
import random
import time
timeout = 20
count = 0
print('отделять НЕ запятой, А точкой')
start_time = time.time()
while True:
num = 0
num_one = random.randint(1, 50)
num_two = random.randint(1, 50)
operation = random.randint(1, 3)
if operation == 1:
operation = '+'
num = num_one + num_two
elif operation == 2:
operation = '-'
num = num_one - num_two
elif operation == 3:
operation = '*'
num = num_one * num_two
print(num_one, operation, num_two, end=' = ')
current_time = time.time()
if current_time - start_time >= timeout:
print('\nВРЕМЯ ВЫШЛО!')
break
user = int(input())
if user == num:
print('\nВЕРНО!\n')
count += 10
timeout -= 0.01
elif user == 0:
break
else:
break
file = open("win.txt", "r")
best = file.read()
if count > int(best):
file_open = open("win.txt", "w")
file_open.write(str(count))
file_open.close()
print(f'Увы, вы проиграли. Ваш рекорд: {count}')
print('Лучший рекорд:', best)
r = input()
if r == 'r':
re = open('win.txt', 'w')
re.write(str(0))
file.close()
Похожие вопросы
- Помогите с задачей python
- Зачем сисадмину php/python?
- Помогите решить задачу на Python.
- Помогите пожалуйста написать программу на языке python
- Помогите, пожалуйста, программа Python
- Какой первый язык программирования учить? Python?
- Что лучше python, C# или же Java
- Решите задачу про прогрессивный налог python
- Вопрос про Python
- Вычислить и вывести на экран в виде таблицы значения функции Python