0
進口讀取文件到我的函數,我想隨機我的函數,但我的讀取文件不是隨機random.choice。請幫我弄清楚,一直在努力工作好幾天沒有幫助。你會怎麼做呢?函數隨機但文本文件總是從頭開始
## Program Name:
## Date: 10/22/2015
## Developer: Arthur Winiarz
import random
QUIZ_FILE = open("Questions.txt","r")
correct = 0
def intro(start):
if start == "yes" or start == "y":
pass
def question1():
global correct
print (QUIZ_FILE.readline())
print (QUIZ_FILE.readline())
print (QUIZ_FILE.readline())
print (QUIZ_FILE.readline())
ans = input()
while ans == "A" or ans == "a":
print("Correct")
correct +=1
break
else:
print ("Incorrect A is the right answer")
return correct
def question2():
global correct
print (QUIZ_FILE.readline())
ans = input()
while ans == "T" or ans == "t":
print("Correct")
correct +=1
break
else:
print ("Incorrect")
return correct
def question3():
global correct
print (QUIZ_FILE.readline())
print (QUIZ_FILE.readline())
print (QUIZ_FILE.readline())
print (QUIZ_FILE.readline())
ans = input()
while ans == "C" or ans == "c":
print("Correct")
correct +=1
break
else:
print ("Incorrect C is the right answer")
return correct
def question4():
global correct
print (QUIZ_FILE.readline())
ans = input()
while ans == "F" or ans == "f":
print("Correct")
correct +=1
break
else:
print ("Incorrect")
return correct
def question5():
global correct
print (QUIZ_FILE.readline())
print (QUIZ_FILE.readline())
print (QUIZ_FILE.readline())
print (QUIZ_FILE.readline())
ans = input()
while ans == "B" or ans == "b":
print("Correct")
correct +=1
break
else:
print ("Incorrect B is the right answer")
return correct
def question6():
global correct
print (QUIZ_FILE.readline())
ans = input()
while ans == "T" or ans == "t":
print("Correct")
correct +=1
break
else:
print ("Incorrect")
return correct
def main():
print("Do you want to start the Quiz?")
start = input()
intro(start)
List = [question1,question2,question3,question4,question5,question6,
question7,question8,question9,question10,question11,question12,
question13,question14,question15,question16,question17,question18,
question19,question20,question21,question22,question23,question24,
question25,question26,question27,question28,question29,question30,
question31,question32,question33,question34,question35,question36,
question37,question38,question39,question40]
random.choice(List)()
print("You have",correct,"correct out of 40")
main()
看起來更像嚴格需要的代碼。你可以進一步減少代碼,直到[在仍然遇到問題時無法刪除代碼](http://stackoverflow.com/help/mcve)? –
我的功能,我有一個開放的讀取文件,讀取測驗的問題。但是,當我嘗試隨機選擇我的列表時,讀取的文件行不會保留在代碼的功能塊中。它們仍然以與讀取文件中相同的順序顯示。如何隨機整個功能,並使正確的讀取文件保留在每個功能 –
我也在考慮從我的文本文件中讀取每一行,並將其保存爲自己的記事本文件並標記並導入它們,但我想要首先嚐試這種方式,全部從一個記事本文件中 –