2015-10-23 23 views
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() 
+0

看起來更像嚴格需要的代碼。你可以進一步減少代碼,直到[在仍然遇到問題時無法刪除代碼](http://stackoverflow.com/help/mcve)? –

+0

我的功能,我有一個開放的讀取文件,讀取測驗的問題。但是,當我嘗試隨機選擇我的列表時,讀取的文件行不會保留在代碼的功能塊中。它們仍然以與讀取文件中相同的順序顯示。如何隨機整個功能,並使正確的讀取文件保留在每個功能 –

+0

我也在考慮從我的文本文件中讀取每一行,並將其保存爲自己的記事本文件並標記並導入它們,但我想要首先嚐試這種方式,全部從一個記事本文件中 –

回答

0

有幾個問題。首先是您使用random.choice。這將返回列表中的任何元素,但不會將其刪除。因此,你最終可能會多次提出同樣的問題。

此外,random.choice(List)()將只運行一個問題一次。

你可以去運行:

# This will shuffle the elements of List, so that is in a random order 
random.shuffle(List) 
# and now we can simply run through each question in turn. 
for question in List: 
    question() 

現在,你仍然有正確的順序讀取行的問題。當你用Python定義一個函數時,定義的代碼塊不會立即執行。而是在調用函數時執行。因此,如果您先撥打question6,它將讀取文件的第一行。您可以通過在整個文件中讀取開頭,並使用固定範圍打印功能中的相應行來解決此問題:

with open("Questions.txt","r") as QUIZ_FILE: 
    QUIZ = QUIZ_FILE.read().split() 
... 
def question1(): 
    global correct 
    print (QUIZ[0]) 
    print (QUIZ[1]) 
    print (QUIZ[2]) 
    print (QUIZ[3]) 
    ... 
def question2(): 
    ... 
    print (QUIZ[4]) 
    ... 

等等。