python - Reading a text file, one chunk of n lines at a time -


i new python , have made questionnaire program. there way have program display each question , choices 1 @ time (the next multiple choice question doesn't show unless previous 1 answered)?

i used slicing accomplish this, wondering doing not in practice or has better alternate way?

#opens file questions, , told read via 'r' open("pythonq.txt", "r") f: #reads file line line 'readlines'     variable = f.readlines() #issue no slicing, reading letter letter. #without slicing , split, questions not print per question #the goal slicing readlines read per question, not entire file or first 5 words         line in variable[0:6]: #this strips brackets , splits commas only, otherwise print both                question = line.split(',') #joins elements of list separated comma           print(", ".join(question))  choice1 = eval(input("\nyour choice?: "))  #sliced second question. begins @ 5 provide room in program between first , second question.  line in variable[6:12]:         question = line.split(',')         print(", ".join(question))  choice2 = eval(input("\nyour choice?: "))   line in variable[12:18]:         question = line.split(',')         print(", ".join(question))  choice3 = eval(input("\nyour choice?: "))   line in variable[18:24]:         question = line.split(',')         print(", ".join(question))  choice4 = eval(input("\nyour choice?: "))   line in variable[24:30]:         question = line.split(',')         print(", ".join(question))  choice5 = eval(input("\nyour choice?: "))   line in variable[30:36]:         question = line.split(',')         print(", ".join(question))  choice6 = eval(input("\nyour choice?: "))   line in variable[36:42]:         question = line.split(',')         print(", ".join(question))  choice7 = eval(input("\nyour choice?: "))   line in variable[42:48]:         question = line.split(',')         print(", ".join(question))  choice8 = eval(input("\nyour choice?: "))   line in variable[48:54]:         question = line.split(',')         print(", ".join(question))  choice9 = eval(input("\nyour choice?: "))   line in variable[54:60]:         question = line.split(',')         print(", ".join(question))  choice10 = eval(input("\nyour choice?: "))  #add numbers user typed , assigned variable x x = choice1 + choice2 + choice3 + choice4 + choice5 + choice6 + choice7 + choice8 + choice9 + choice10  #there high person's score can go, depending upon if user chose 1's, 2's, 3's or 4's in quiz. if x <= 13:     print("\n\n\nyour personality , work environment type is...\n \n\n\nrealistic: 'doer'\n") #another file created results. import file. call function, categorya.     import results     results.categorya() elif x <= 22:     print("\n\n\nyour personality , work environment type is...\n \n\n\nsocial: helper\n")     import results     results.categoryb() elif x <= 31:     print("\n\n\nyour personality , work environment type is...\n \n\n\nenterprising: persuader\n")     import results     results.categoryc() elif x <= 40:     print("\n\n\nyour personality , work environment type is...\n \n\n\nconventional: organizer\n")     import results     results.categord() 

sorry, can't question, wanted. still want ask: have 9 same codes, may offer my?

your code repeated:

for line in variable[6:12]:         question = line.split(',')         print(", ".join(question))  choice2 = eval(input("\nyour choice?: ")) 

9 times means loop: if rename "choice1" "x"

for in range(1,10)     line in variable[6*i:6*(i+1)]:         question = line.split(',')         print(", ".join(question))  x+=eval(input("\nyour choice?: ")) 

i accept criticism.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -