我运行代码并且它工作得很好,但它只记录最后一个反应时间而不是所有反应时间。我似乎无法找到什么错误。我建立了另一个类似于此的程序,我检查了两个并进行了比较,但仍然无法正常工作
#Imports
import pygame
from datetime import datetime
import time
import random
from sys import argv
#Builds PyGame Screen
pygame.init()
screen = pygame.display.set_mode((640, 480))
black = (0, 0, 0)
x = float(2)
#For Loop
for turn in range(3):
screen.fill((255, 255, 255))
pygame.display.flip()
#Delay Before The Rest Of The Program Is Runned
time.sleep(x)
#Draws A Black Circle And Starts Runing The Reaction Program
letter = random.randint(1, 26)
if letter == 1:
letter = pygame.K_a
elif letter == 2:
letter = pygame.K_b
elif letter == 3:
letter = pygame.K_c
elif letter == 4:
letter = pygame.K_d
elif letter == 5:
letter = pygame.K_e
elif letter == 6:
letter = pygame.K_f
elif letter == 7:
letter = pygame.K_g
elif letter == 8:
letter = pygame.K_h
elif letter == 9:
letter = pygame.K_i
elif letter == 10:
letter = pygame.K_j
elif letter == 11:
letter = pygame.K_k
elif letter == 12:
letter = pygame.K_l
elif letter == 13:
letter = pygame.K_m
elif letter == 14:
letter = pygame.K_n
elif letter == 15:
letter = pygame.K_o
elif letter == 16:
letter = pygame.K_p
elif letter == 17:
letter = pygame.K_q
elif letter == 18:
letter = pygame.K_r
elif letter == 19:
letter = pygame.K_s
elif letter == 20:
letter = pygame.K_t
elif letter == 21:
letter = pygame.K_u
elif letter == 22:
letter = pygame.K_v
elif letter == 23:
letter = pygame.K_w
elif letter == 24:
letter = pygame.K_x
elif letter == 25:
letter = pygame.K_y
elif letter == 26:
letter = pygame.K_z
font = pygame.font.SysFont("Comic Sans MS", 30)
if letter == pygame.K_a:
label= font.render("A", 1, black)
screen.blit(label, (320, 240))
elif letter == pygame.K_b:
label = font.render("B", 1, black)
screen.blit(label, (320, 240))
elif letter == pygame.K_c:
label = font.render("C", 1, black)
screen.blit(label, (320, 240))
elif letter == pygame.K_d:
label = font.render("D", 1, black)
screen.blit(label, (320, 240))
elif letter == pygame.K_e:
label = font.render("E", 1, black)
screen.blit(label, (320, 240))
elif letter == pygame.K_f:
label = font.render("F", 1, black)
screen.blit(label, (320, 240))
elif letter == pygame.K_g:
label = font.render("G", 1, black)
screen.blit(label, (320, 240))
elif letter == pygame.K_h:
label = font.render("H", 1, black)
screen.blit(label, (320, 240))
elif letter == pygame.K_i:
label = font.render("I", 1, black)
screen.blit(label, (320, 240))
elif letter == pygame.K_j:
label = font.render("J", 1, black)
screen.blit(label, (320, 240))
elif letter == pygame.K_k:
label = font.render("K", 1, black)
screen.blit(label, (320, 240))
elif letter == pygame.K_l:
label = font.render("L", 1, black)
screen.blit(label, (320, 240))
elif letter == pygame.K_m:
label = font.render("M", 1, black)
screen.blit(label, (320, 240))
elif letter == pygame.K_n:
label = font.render("N", 1, black)
screen.blit(label, (320, 240))
elif letter == pygame.K_o:
label = font.render("O", 1, black)
screen.blit(label, (320, 240))
elif letter == pygame.K_p:
label = font.render("P", 1, black)
screen.blit(label, (320, 240))
elif letter == pygame.K_q:
label = font.render("Q", 1, black)
screen.blit(label, (320, 240))
elif letter == pygame.K_r:
label = font.render("R", 1, black)
screen.blit(label, (320, 240))
elif letter == pygame.K_s:
label = font.render("S", 1, black)
screen.blit(label, (320, 240))
elif letter == pygame.K_t:
label = font.render("T", 1, black)
screen.blit(label, (320, 240))
elif letter == pygame.K_u:
label = font.render("U", 1, black)
screen.blit(label, (320, 240))
elif letter == pygame.K_v:
label = font.render("V", 1, black)
screen.blit(label, (320, 240))
elif letter == pygame.K_w:
label = font.render("W", 1, black)
screen.blit(label, (320, 240))
elif letter == pygame.K_x:
label = font.render("X", 1, black)
screen.blit(label, (320, 240))
elif letter == pygame.K_y:
label = font.render("Y", 1, black)
screen.blit(label, (320, 240))
elif letter == pygame.K_z:
label = font.render("Z", 1, black)
screen.blit(label, (320, 240))
#Opens Output File And Stores Reaction Times There
reactiontimes = open("z output.txt", "w")
pygame.display.flip()
ReactionStart= datetime.now()
reactiontimes.write(str(ReactionStart) + "\n")
print (ReactionStart)
#Checks to see if key pushed is correct
true = True
while true:
for event in pygame.event.get():
if (event.type == pygame.KEYDOWN):
if (event.key == letter):
ReactionEnd = datetime.now()
reactiontimes.write(str(ReactionEnd) + "\n")
print (ReactionEnd)
true = False
#Calculates Actual Reaction Time Then Delays To Reapeat
ReactionTime = ReactionEnd - ReactionStart
reactiontimes.write(str(ReactionTime) + "\n")
print(ReactionTime)
#Closes Document
reactiontimes.close()
#Checks To See If Program Is Quit
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
#
#
答案 0 :(得分:0)
w
附加到您的文件时, a
会覆盖您的内容。
`open("z output.txt", "a")`
您还应该使用以打开文件,因为它会自动关闭它们:
`with open("z output.txt", "w") as reactiontimes:`