저는 Python을 처음 사용하고 질문지 프로그램을 만들었습니다. 각 질문과 선택 사항을 한 번에 하나씩 표시하도록 할 수있는 방법이 있습니까 (이전 선택 질문에 답하지 않는 한 다음 객관식 질문은 표시되지 않습니다)?한 번에 n 줄씩 텍스트 파일 읽기
나는 이것을 수행하기 위해 슬라이싱을 사용했지만 좋은 연습이 아니거나 더 나은 대체 방법이있는 무언가를하고 있는지 궁금합니다.
#opens the file with questions, and is told to read only via 'r'
with open("PythonQ.txt", "r") as f:
#reads the file line by line with 'readlines'
variable = f.readlines()
#issue was with no slicing, it was reading letter by letter. so
#without the slicing and the split, the questions would not print per question
#the goal by slicing is to get readlines to read per question, not the entire file or first 5 words
for line in variable[0:6]:
#this strips the brackets and splits it by commas only, otherwise it will print both
question = line.split(',')
#joins elements of the list which are separated by a comma
print(", ".join(question))
choice1 = eval(input("\nYour choice?: "))
#sliced for second question. Begins at five to provide room in program between first and second question.
for line in variable[6:12]:
question = line.split(',')
print(", ".join(question))
choice2 = eval(input("\nYour choice?: "))
for line in variable[12:18]:
question = line.split(',')
print(", ".join(question))
choice3 = eval(input("\nYour choice?: "))
for line in variable[18:24]:
question = line.split(',')
print(", ".join(question))
choice4 = eval(input("\nYour choice?: "))
for line in variable[24:30]:
question = line.split(',')
print(", ".join(question))
choice5 = eval(input("\nYour choice?: "))
for line in variable[30:36]:
question = line.split(',')
print(", ".join(question))
choice6 = eval(input("\nYour choice?: "))
for line in variable[36:42]:
question = line.split(',')
print(", ".join(question))
choice7 = eval(input("\nYour choice?: "))
for line in variable[42:48]:
question = line.split(',')
print(", ".join(question))
choice8 = eval(input("\nYour choice?: "))
for line in variable[48:54]:
question = line.split(',')
print(", ".join(question))
choice9 = eval(input("\nYour choice?: "))
for line in variable[54:60]:
question = line.split(',')
print(", ".join(question))
choice10 = eval(input("\nYour choice?: "))
#add up all the numbers the user typed and assigned it variable x
x = choice1 + choice2 + choice3 + choice4 + choice5 + choice6 + choice7 + choice8 + choice9 + choice10
#there are only so high a person's score can go, depending upon if user chose mostly 1's, 2's, 3's or 4's in quiz.
if x <= 13:
print("\n\n\nYour personality and work environment type is...\n \n\n\nRealistic: The 'Doer'\n")
#another file was created for the results. Import file. Then call the function, CategoryA.
import Results
Results.CategoryA()
elif x <= 22:
print("\n\n\nYour personality and work environment type is...\n \n\n\nSocial: The Helper\n")
import Results
Results.CategoryB()
elif x <= 31:
print("\n\n\nYour personality and work environment type is...\n \n\n\nEnterprising: The Persuader\n")
import Results
Results.CategoryC()
elif x <= 40:
print("\n\n\nYour personality and work environment type is...\n \n\n\nConventional: The Organizer\n")
import Results
Results.CategorD()
읽고있는 파일의 예가 있습니까 (PythonQ.txt)? –
OP와 같은데 전체 파일을 읽습니다. 6 행의 객관식 질문의 퀴즈로 구성되어 있습니다. 1) .. 4) :'variable = f.readlines()' – smci
'question = line .split (',')'바로 뒤에'print (",".join (question) "? 불필요한 것 같습니다. – smci