Викторина и модуль random
import random,time
start_time = time.monotonic()
correct = 0
q1 = 'The epoch is the point where the time starts, and is platform dependent. For Unix, the epoch is January 1, 1970, 00:00:00 (UTC). To find out what the epoch is on a given platform, look at time.gmtime(0).'
q2 = 'The functions in this module may not handle dates and times before the epoch or far in the future. The cut-off point in the future is determined by the C library; for 32-bit systems, it is typically in 2038.'
q3 = 'UTC is Coordinated Universal Time (formerly known as Greenwich Mean Time, or GMT). The acronym UTC is not a mistake but a compromise between English and French.'
q4 = 'Function strptime() can parse 2-digit years when given %y format code. When 2-digit years are parsed, they are converted according to the POSIX and ISO C standards: values 69–99 are mapped to 1969–1999, and values 0–68 are mapped to 2000–2068.'
all_answers = q1,q2,q3,q4
for _ in range(4):
print(random.choice(all_answers))
ask = input('T or F? ')
if ask == 'T':
print('Correct!, +1')
correct += 1
else:
print('uncorrectly')
print('Результат = ' + str(correct))
print(f'Тест пройден за {int(time.monotonic() - start_time)} секунд')
Важно , чтобы вопросы шли в рандомном порядке и не повторялись. Но в моём коде вопросы могут повторяться по 2-3 раза, каким образом можно это исправить?
Ответы (2 шт):
Автор решения: Vektor Valentine
→ Ссылка
Вот так можно:
# Формируешь список вопросов:
all_answers = [q1,q2,q3,q4]
#Вариант первый -> Берешь рандомного из "колоды":
while(len(all_ansewrs) != 0):
print(all_answers.pop(random.randint(0, len(all_answers)-1)))
#Вариант второй -> Замешиваешь "колоду" каждый раз, когда берешь из:
while(len(all_answers) != 0):
random.shuffle(all_answers)
print(all_answers.pop())
В вашем варианте ви просто печатали из списка случайный елемент (никак не изменяя список при этом):
print(random.choice(all_answers))
Автор решения: MarianD
→ Ссылка
Используйте функцию random.shuffle() чтобы заранее изменить порядок вопросов.
Значит, вместо вашего
all_answers = q1,q2,q3,q4
for _ in range(4):
print(random.choice(all_answers))
примените
all_questions = [q1, q2, q3, q4]
random.shuffle(all_questions)
for question in all_questions:
print(question)