python练习9
电子算盘1.py
i
import random
import time
from typing import Tuple
class SorobanTester:
ADDITION = '+'
SUBTRACTION = '-'
OPERATIONS = [ADDITION, SUBTRACTION]
MAX_NUMBER = 100
def __init__(self):
self.total_count = 0
self.correct_count = 0
self.username = ''
self.questions = []
self.answers = []
self.is_running = False
self.start_time = None
self.end_time = None
def generate_questions(self, count: int) -> None:
self.questions = []
self.answers = []
for i in range(count):
a = random.randint(1, self.MAX_NUMBER)
b = random.randint(1, self.MAX_NUMBER)
op = random.choice(self.OPERATIONS)
question = f'{a} {op} {b} = '
if op == self.ADDITION:
answer = a + b
else:
answer = a - b
self.questions.append(question)
self.answers.append(answer)
def start(self, username: str, count: int) -> None:
self.username = username
self.total_count = count
self.generate_questions(count)
self.is_running = True
self.start_time = time.time()
def stop(self) -> None:
self.is_running = False
self.end_time = time.time()
self._show_result()
def submit_answer(self, answer: int) -> bool:
if not self.is_running:
return False
if answer == self.answers[self.correct_count]:
self.correct_count += 1
return True
else:
return False
def _show_result(self) -> None:
print(f'Result for {self.username}:')
print(f'Total: {self.total_count}')
print(f'Correct: {self.correct_count}')
print(f'Incorrect: {self.total_count - self.correct_count}')
print(f'Time: {self.end_time - self.start_time:.2f}s')
if self.total_count > 0:
score = (self.correct_count / self.total_count) * 100
print(f'Score: {score:.2f}%')
def main():
tester = SorobanTester()
while True:
print('------------------------------------')
print('1. Start test')
print('2. Quit')
choice = input('Please choose: ')
if choice == '1':
username = input('Please input your name: ')
count = int(input('Please input the number of questions: '))
tester.start(username, count)
for i in range(count):
question = tester.questions[i]
print(f'Question {i+1}: {question}')
answer = int(input('Please input your answer: '))
if tester.submit_answer(answer):
print('Correct')
else:
print('Incorrect')
tester.stop()
elif choice == '2':
break
else:
print('Invalid choice')
if __name__ == '__main__':
main()
import time
from typing import Tuple
class SorobanTester:
ADDITION = '+'
SUBTRACTION = '-'
OPERATIONS = [ADDITION, SUBTRACTION]
MAX_NUMBER = 100
def __init__(self):
self.total_count = 0
self.correct_count = 0
self.username = ''
self.questions = []
self.answers = []
self.is_running = False
self.start_time = None
self.end_time = None
def generate_questions(self, count: int) -> None:
self.questions = []
self.answers = []
for i in range(count):
a = random.randint(1, self.MAX_NUMBER)
b = random.randint(1, self.MAX_NUMBER)
op = random.choice(self.OPERATIONS)
question = f'{a} {op} {b} = '
if op == self.ADDITION:
answer = a + b
else:
answer = a - b
self.questions.append(question)
self.answers.append(answer)
def start(self, username: str, count: int) -> None:
self.username = username
self.total_count = count
self.generate_questions(count)
self.is_running = True
self.start_time = time.time()
def stop(self) -> None:
self.is_running = False
self.end_time = time.time()
self._show_result()
def submit_answer(self, answer: int) -> bool:
if not self.is_running:
return False
if answer == self.answers[self.correct_count]:
self.correct_count += 1
return True
else:
return False
def _show_result(self) -> None:
print(f'Result for {self.username}:')
print(f'Total: {self.total_count}')
print(f'Correct: {self.correct_count}')
print(f'Incorrect: {self.total_count - self.correct_count}')
print(f'Time: {self.end_time - self.start_time:.2f}s')
if self.total_count > 0:
score = (self.correct_count / self.total_count) * 100
print(f'Score: {score:.2f}%')
def main():
tester = SorobanTester()
while True:
print('------------------------------------')
print('1. Start test')
print('2. Quit')
choice = input('Please choose: ')
if choice == '1':
username = input('Please input your name: ')
count = int(input('Please input the number of questions: '))
tester.start(username, count)
for i in range(count):
question = tester.questions[i]
print(f'Question {i+1}: {question}')
answer = int(input('Please input your answer: '))
if tester.submit_answer(answer):
print('Correct')
else:
print('Incorrect')
tester.stop()
elif choice == '2':
break
else:
print('Invalid choice')
if __name__ == '__main__':
main()
 
                    
                     
                    
                 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号