Jacob_DiDiodato icon

Survey backbone

Jacob_DiDiodato | PRO | 08/09/17 01:49:42 AM UTC | 0 ⭐ | 352 👁️ | Never ⏰ | []
Python |

8.61 KB

|

None

|

0 👍

/

0 👎

import pygame, easygui, sys, random
 
def main():
    #All font and text
    font = pygame.font.SysFont("times", 30)
    choiceALabel = font.render("Choice A", 1, (255,255,255))
    choiceBLabel = font.render("Choice B", 1, (255,255,255))
    submitLabel = font.render("Pick one ->", 1, (255,255,255))
    #End of font and text
    
    attributesList = []
    levelsDict = {}
    attributesSurfaces = []
    levelsSurfaces = []
    excelInformation = {}
    trial = 0
    numTrials = 0
    attributesPerPage = 0
    try:
        attributesFile = open("surveyinfo.txt", "r")
        fileContents = attributesFile.read().split("\n")
        #Checking to see if the file contains the magic number
        if fileContents[0] != "040":
            easygui.msgbox(msg="Survey Error. File not setup properly. Please use the conjointSDT.py and export to Jacob-formatted TXT.",title="Error")
            return
        fileContentsNoMagicNumber = fileContents[1:]
        #Iterate everything besides the magic number to build the attributes and levels
        for i in range(len(fileContentsNoMagicNumber)):
            if i != len(fileContentsNoMagicNumber) - 1:
                attributesList.append(fileContentsNoMagicNumber[i].split(":")[0])
                levelsDict[attributesList[i]] = fileContentsNoMagicNumber[i].split(":")[1].split(",")
            else:
                numTrials = int(fileContentsNoMagicNumber[i])
    #If the file doesn't exist then exit the program
    except IOError:
        easygui.msgbox(msg="Survey Error. Missing file \"surveyinfo.txt\".",title="Error")
        return
    
    if len(attributesList) > 5:
        attributesPerPage = 5
    else:
        attributesPerPage = len(attributesList)
        
    #print attributesPerPage
 
    clock = pygame.time.Clock()
 
    screen = pygame.display.set_mode((640,480), pygame.NOFRAME)
    
    #Here we set a unique userID
    userID = 0
    try:
        #Here we read from the file if we can and get the unique ID of the user
        userIDFile = open("id.txt", "r")
        userID = int(userIDFile.read())
        userIDFile.close()
    except IOError:
        #If the file doesn't exist then we pass by the exception because it's not terrible
        pass
    
    #Here we replace the contents of the old file with a new userID 
    userIDFile = open("id.txt", "w")
    userIDFile.write(str(userID+1))
    userIDFile.close()
    #End of unique userID
    
    #Early questions here
    ##firstName = easygui.enterbox("Please enter your first name.")
    ##lastName = easygui.enterbox("Please enter your last name.")
    ##age = int(easygui.enterbox("Please enter your age."))
    #End of early questions
    
    running = True
    createPage = True
    aFirst = True
    bFirst = False
    while running:
        if createPage:
            isAFirst = random.randint(0,1)
            if isAFirst:
                aFirst = True
                bFirst = False
            else:
                aFirst = False
                bFirst = True
            chosenAttributes = random.sample(attributesList, attributesPerPage)
            chosenLevelsForA = []
            chosenLevelsForB = []
            for i in range(attributesPerPage):
                chosenLevelsForA.append(levelsDict[chosenAttributes[i]][random.randint(0,len(levelsDict[chosenAttributes[i]])) - 1])
                chosenLevelsForB.append(levelsDict[chosenAttributes[i]][random.randint(0,len(levelsDict[chosenAttributes[i]])) - 1])
            attributesSurfaces = []
            levelsSurfacesForA = []
            levelsSurfacesForB = []
            for i in range(attributesPerPage):
                attributesSurfaces.append(font.render(chosenAttributes[i], 1, (255,255,255)))
                levelsSurfacesForA.append(font.render(chosenLevelsForA[i], 1, (255,255,255)))
                levelsSurfacesForB.append(font.render(chosenLevelsForB[i], 1, (255,255,255)))
            createPage = False
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.MOUSEBUTTONUP:
                mousePos = pygame.mouse.get_pos()
                #Getting the click for the radio buttons
                outcome = 0
                if mousePos[0] >= 350 and mousePos[0] <= 370 and mousePos[1] >= 445 and mousePos[1] <= 465: #First button
                    if aFirst == True:
                        outcome = 1
                    else:
                        outcome = 2
                elif mousePos[0] >= 535 and mousePos[0] <= 555 and mousePos[1] >= 445 and mousePos[1] <= 465: #Second button
                    if aFirst == True:
                        outcome = 3
                    else:
                        outcome = 4
                
                excelInformation["Trial " + str(trial + 1)] = {}
                for i in range(len(attributesList)):
                    excelInformation["Trial " + str(trial + 1)][attributesList[i]] = ""
                excelInformation["Trial " + str(trial + 1)]["Option Chosen"] = ""
                excelInformation["Trial " + str(trial + 1)]["Neither?"] = ""
                    
                print "Outcome: " + str(outcome)
                
                if outcome == 1:#Picked B when B was first
                    for i in range(len(chosenAttributes)):
                        #print chosenAttributes[i] + ":" + chosenLevelsForA[i]#Wrong but it works
                        excelInformation["Trial " + str(trial + 1)][chosenAttributes[i]] = chosenLevelsForA[i]
                    keepOption = easygui.ynbox("You have chosen Option B. If given the option between option B and no option would you still choose option B?", "Confirmation Screen", ("Yes", "No"))
                    excelInformation["Trial " + str(trial + 1)]["Option Chosen"] = "B"
                    if keepOption:
                        excelInformation["Trial " + str(trial + 1)]["Neither?"] = False
                    else:
                        excelInformation["Trial " + str(trial + 1)]["Neither?"] = True
                elif outcome == 2:#Picked A when A was first
                    for i in range(len(chosenAttributes)):
                        #print chosenAttributes[i] + ":" + chosenLevelsForA[i]
                        excelInformation["Trial " + str(trial + 1)][chosenAttributes[i]] = chosenLevelsForA[i]
                    keepOption = easygui.ynbox("You have chosen Option A. If given the option between option A and no option would you still choose option A?", "Confirmation Screen", ("Yes", "No"))
                    excelInformation["Trial " + str(trial + 1)]["Option Chosen"] = "A"
                    if keepOption:
                        excelInformation["Trial " + str(trial + 1)]["Neither?"] = False
                    else:
                        excelInformation["Trial " + str(trial + 1)]["Neither?"] = True
                elif outcome == 3:#Picked A when B was first
                    for i in range(len(chosenAttributes)):
                        #print chosenAttributes[i] + ":" + chosenLevelsForB[i]#Wrong but it works
                        excelInformation["Trial " + str(trial + 1)][chosenAttributes[i]] = chosenLevelsForB[i]
                    keepOption = easygui.ynbox("You have chosen Option A. If given the option between option A and no option would you still choose option A?", "Confirmation Screen", ("Yes", "No"))
                    excelInformation["Trial " + str(trial + 1)]["Option Chosen"] = "A"
                    if keepOption:
                        excelInformation["Trial " + str(trial + 1)]["Neither?"] = False
                    else:
                        excelInformation["Trial " + str(trial + 1)]["Neither?"] = True
                elif outcome == 4:#Picked B when A was first
                    for i in range(len(chosenAttributes)):
                        #print chosenAttributes[i] + ":" + chosenLevelsForB[i]
                        excelInformation["Trial " + str(trial + 1)][chosenAttributes[i]] = chosenLevelsForB[i]
                    keepOption = easygui.ynbox("You have chosen Option B. If given the option between option B and no option would you still choose option B?", "Confirmation Screen", ("Yes", "No"))
                    excelInformation["Trial " + str(trial + 1)]["Option Chosen"] = "B"
                    if keepOption:
                        excelInformation["Trial " + str(trial + 1)]["Neither?"] = False
                    else:
                        excelInformation["Trial " + str(trial + 1)]["Neither?"] = True
                trial += 1
                createPage = True
                if trial == numTrials:
                    running = False
                    print excelInformation
        #Drawing the UI
        screen.fill((211,211,211))
        pygame.draw.line(screen, (0,0,0), (453, 0), (453, 480))
        pygame.draw.line(screen, (0,0,0), (266, 0), (266, 480))
        pygame.draw.line(screen, (0,0,0), (0, 72), (640, 72))
        pygame.draw.line(screen, (0,0,0), (0, 144), (640, 144))
        pygame.draw.line(screen, (0,0,0), (0, 216), (640, 216))
        pygame.draw.line(screen, (0,0,0), (0, 288), (640, 288))
        pygame.draw.line(screen, (0,0,0), (0, 360), (640, 360))
        pygame.draw.line(screen, (0,0,0), (0, 432), (640, 432))
        pygame.draw.ellipse(screen, (255,255,255), (350,445,20,20))
        pygame.draw.ellipse(screen, (0,0,0), (349,444,22,22), 1)
        pygame.draw.ellipse(screen, (255,255,255), (535,445,20,20))
        pygame.draw.ellipse(screen, (0,0,0), (534,444,22,22), 1)
        screen.blit(choiceALabel, (303 + int(aFirst)*187, 20))
        screen.blit(choiceBLabel, (303 + int(bFirst)*187, 20))
        screen.blit(submitLabel, (screen.get_width()/5 - submitLabel.get_width()/2, screen.get_height() - 40))
        
        #print chosenAttributes
        #print chosenLevels
        
        for i in range(0, attributesPerPage):
            screen.blit(attributesSurfaces[i], (5, 90 + 72*i))
            screen.blit(levelsSurfacesForA[i], (360 - levelsSurfacesForA[i].get_width()/2, 90 + 72*i))
            screen.blit(levelsSurfacesForB[i], (545 - levelsSurfacesForB[i].get_width()/2, 90 + 72*i))
    
        pygame.display.update()
        
        msElapsed = clock.tick(60)
 
if __name__ == "__main__":
    pygame.init()
    main()
    pygame.quit()

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎