Jacob_DiDiodato

Survey backbone

Aug 8th, 2017
394
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.61 KB | None | 0 0
  1. import pygame, easygui, sys, random
  2.  
  3. def main():
  4.     #All font and text
  5.     font = pygame.font.SysFont("times", 30)
  6.     choiceALabel = font.render("Choice A", 1, (255,255,255))
  7.     choiceBLabel = font.render("Choice B", 1, (255,255,255))
  8.     submitLabel = font.render("Pick one ->", 1, (255,255,255))
  9.     #End of font and text
  10.    
  11.     attributesList = []
  12.     levelsDict = {}
  13.     attributesSurfaces = []
  14.     levelsSurfaces = []
  15.     excelInformation = {}
  16.     trial = 0
  17.     numTrials = 0
  18.     attributesPerPage = 0
  19.     try:
  20.         attributesFile = open("surveyinfo.txt", "r")
  21.         fileContents = attributesFile.read().split("\n")
  22.         #Checking to see if the file contains the magic number
  23.         if fileContents[0] != "040":
  24.             easygui.msgbox(msg="Survey Error. File not setup properly. Please use the conjointSDT.py and export to Jacob-formatted TXT.",title="Error")
  25.             return
  26.         fileContentsNoMagicNumber = fileContents[1:]
  27.         #Iterate everything besides the magic number to build the attributes and levels
  28.         for i in range(len(fileContentsNoMagicNumber)):
  29.             if i != len(fileContentsNoMagicNumber) - 1:
  30.                 attributesList.append(fileContentsNoMagicNumber[i].split(":")[0])
  31.                 levelsDict[attributesList[i]] = fileContentsNoMagicNumber[i].split(":")[1].split(",")
  32.             else:
  33.                 numTrials = int(fileContentsNoMagicNumber[i])
  34.     #If the file doesn't exist then exit the program
  35.     except IOError:
  36.         easygui.msgbox(msg="Survey Error. Missing file \"surveyinfo.txt\".",title="Error")
  37.         return
  38.    
  39.     if len(attributesList) > 5:
  40.         attributesPerPage = 5
  41.     else:
  42.         attributesPerPage = len(attributesList)
  43.        
  44.     #print attributesPerPage
  45.  
  46.     clock = pygame.time.Clock()
  47.  
  48.     screen = pygame.display.set_mode((640,480), pygame.NOFRAME)
  49.    
  50.     #Here we set a unique userID
  51.     userID = 0
  52.     try:
  53.         #Here we read from the file if we can and get the unique ID of the user
  54.         userIDFile = open("id.txt", "r")
  55.         userID = int(userIDFile.read())
  56.         userIDFile.close()
  57.     except IOError:
  58.         #If the file doesn't exist then we pass by the exception because it's not terrible
  59.         pass
  60.    
  61.     #Here we replace the contents of the old file with a new userID
  62.     userIDFile = open("id.txt", "w")
  63.     userIDFile.write(str(userID+1))
  64.     userIDFile.close()
  65.     #End of unique userID
  66.    
  67.     #Early questions here
  68.     ##firstName = easygui.enterbox("Please enter your first name.")
  69.     ##lastName = easygui.enterbox("Please enter your last name.")
  70.     ##age = int(easygui.enterbox("Please enter your age."))
  71.     #End of early questions
  72.    
  73.     running = True
  74.     createPage = True
  75.     aFirst = True
  76.     bFirst = False
  77.     while running:
  78.         if createPage:
  79.             isAFirst = random.randint(0,1)
  80.             if isAFirst:
  81.                 aFirst = True
  82.                 bFirst = False
  83.             else:
  84.                 aFirst = False
  85.                 bFirst = True
  86.             chosenAttributes = random.sample(attributesList, attributesPerPage)
  87.             chosenLevelsForA = []
  88.             chosenLevelsForB = []
  89.             for i in range(attributesPerPage):
  90.                 chosenLevelsForA.append(levelsDict[chosenAttributes[i]][random.randint(0,len(levelsDict[chosenAttributes[i]])) - 1])
  91.                 chosenLevelsForB.append(levelsDict[chosenAttributes[i]][random.randint(0,len(levelsDict[chosenAttributes[i]])) - 1])
  92.             attributesSurfaces = []
  93.             levelsSurfacesForA = []
  94.             levelsSurfacesForB = []
  95.             for i in range(attributesPerPage):
  96.                 attributesSurfaces.append(font.render(chosenAttributes[i], 1, (255,255,255)))
  97.                 levelsSurfacesForA.append(font.render(chosenLevelsForA[i], 1, (255,255,255)))
  98.                 levelsSurfacesForB.append(font.render(chosenLevelsForB[i], 1, (255,255,255)))
  99.             createPage = False
  100.         for event in pygame.event.get():
  101.             if event.type == pygame.QUIT:
  102.                 running = False
  103.             if event.type == pygame.MOUSEBUTTONUP:
  104.                 mousePos = pygame.mouse.get_pos()
  105.                 #Getting the click for the radio buttons
  106.                 outcome = 0
  107.                 if mousePos[0] >= 350 and mousePos[0] <= 370 and mousePos[1] >= 445 and mousePos[1] <= 465: #First button
  108.                     if aFirst == True:
  109.                         outcome = 1
  110.                     else:
  111.                         outcome = 2
  112.                 elif mousePos[0] >= 535 and mousePos[0] <= 555 and mousePos[1] >= 445 and mousePos[1] <= 465: #Second button
  113.                     if aFirst == True:
  114.                         outcome = 3
  115.                     else:
  116.                         outcome = 4
  117.                
  118.                 excelInformation["Trial " + str(trial + 1)] = {}
  119.                 for i in range(len(attributesList)):
  120.                     excelInformation["Trial " + str(trial + 1)][attributesList[i]] = ""
  121.                 excelInformation["Trial " + str(trial + 1)]["Option Chosen"] = ""
  122.                 excelInformation["Trial " + str(trial + 1)]["Neither?"] = ""
  123.                    
  124.                 print "Outcome: " + str(outcome)
  125.                
  126.                 if outcome == 1:#Picked B when B was first
  127.                     for i in range(len(chosenAttributes)):
  128.                         #print chosenAttributes[i] + ":" + chosenLevelsForA[i]#Wrong but it works
  129.                         excelInformation["Trial " + str(trial + 1)][chosenAttributes[i]] = chosenLevelsForA[i]
  130.                     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"))
  131.                     excelInformation["Trial " + str(trial + 1)]["Option Chosen"] = "B"
  132.                     if keepOption:
  133.                         excelInformation["Trial " + str(trial + 1)]["Neither?"] = False
  134.                     else:
  135.                         excelInformation["Trial " + str(trial + 1)]["Neither?"] = True
  136.                 elif outcome == 2:#Picked A when A was first
  137.                     for i in range(len(chosenAttributes)):
  138.                         #print chosenAttributes[i] + ":" + chosenLevelsForA[i]
  139.                         excelInformation["Trial " + str(trial + 1)][chosenAttributes[i]] = chosenLevelsForA[i]
  140.                     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"))
  141.                     excelInformation["Trial " + str(trial + 1)]["Option Chosen"] = "A"
  142.                     if keepOption:
  143.                         excelInformation["Trial " + str(trial + 1)]["Neither?"] = False
  144.                     else:
  145.                         excelInformation["Trial " + str(trial + 1)]["Neither?"] = True
  146.                 elif outcome == 3:#Picked A when B was first
  147.                     for i in range(len(chosenAttributes)):
  148.                         #print chosenAttributes[i] + ":" + chosenLevelsForB[i]#Wrong but it works
  149.                         excelInformation["Trial " + str(trial + 1)][chosenAttributes[i]] = chosenLevelsForB[i]
  150.                     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"))
  151.                     excelInformation["Trial " + str(trial + 1)]["Option Chosen"] = "A"
  152.                     if keepOption:
  153.                         excelInformation["Trial " + str(trial + 1)]["Neither?"] = False
  154.                     else:
  155.                         excelInformation["Trial " + str(trial + 1)]["Neither?"] = True
  156.                 elif outcome == 4:#Picked B when A was first
  157.                     for i in range(len(chosenAttributes)):
  158.                         #print chosenAttributes[i] + ":" + chosenLevelsForB[i]
  159.                         excelInformation["Trial " + str(trial + 1)][chosenAttributes[i]] = chosenLevelsForB[i]
  160.                     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"))
  161.                     excelInformation["Trial " + str(trial + 1)]["Option Chosen"] = "B"
  162.                     if keepOption:
  163.                         excelInformation["Trial " + str(trial + 1)]["Neither?"] = False
  164.                     else:
  165.                         excelInformation["Trial " + str(trial + 1)]["Neither?"] = True
  166.                 trial += 1
  167.                 createPage = True
  168.                 if trial == numTrials:
  169.                     running = False
  170.                     print excelInformation
  171.         #Drawing the UI
  172.         screen.fill((211,211,211))
  173.         pygame.draw.line(screen, (0,0,0), (453, 0), (453, 480))
  174.         pygame.draw.line(screen, (0,0,0), (266, 0), (266, 480))
  175.         pygame.draw.line(screen, (0,0,0), (0, 72), (640, 72))
  176.         pygame.draw.line(screen, (0,0,0), (0, 144), (640, 144))
  177.         pygame.draw.line(screen, (0,0,0), (0, 216), (640, 216))
  178.         pygame.draw.line(screen, (0,0,0), (0, 288), (640, 288))
  179.         pygame.draw.line(screen, (0,0,0), (0, 360), (640, 360))
  180.         pygame.draw.line(screen, (0,0,0), (0, 432), (640, 432))
  181.         pygame.draw.ellipse(screen, (255,255,255), (350,445,20,20))
  182.         pygame.draw.ellipse(screen, (0,0,0), (349,444,22,22), 1)
  183.         pygame.draw.ellipse(screen, (255,255,255), (535,445,20,20))
  184.         pygame.draw.ellipse(screen, (0,0,0), (534,444,22,22), 1)
  185.         screen.blit(choiceALabel, (303 + int(aFirst)*187, 20))
  186.         screen.blit(choiceBLabel, (303 + int(bFirst)*187, 20))
  187.         screen.blit(submitLabel, (screen.get_width()/5 - submitLabel.get_width()/2, screen.get_height() - 40))
  188.        
  189.         #print chosenAttributes
  190.         #print chosenLevels
  191.        
  192.         for i in range(0, attributesPerPage):
  193.             screen.blit(attributesSurfaces[i], (5, 90 + 72*i))
  194.             screen.blit(levelsSurfacesForA[i], (360 - levelsSurfacesForA[i].get_width()/2, 90 + 72*i))
  195.             screen.blit(levelsSurfacesForB[i], (545 - levelsSurfacesForB[i].get_width()/2, 90 + 72*i))
  196.    
  197.         pygame.display.update()
  198.        
  199.         msElapsed = clock.tick(60)
  200.  
  201. if __name__ == "__main__":
  202.     pygame.init()
  203.     main()
  204.     pygame.quit()
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment