我是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讀取整個文件,其中包括一個六行的多項選擇題的測驗索引1).. 4):'variable = f.readlines()' – smci
做什麼是'question = line .split(',')'後面跟着'print(「,」.join(問題))'?似乎沒有必要。 – smci