2017-06-20 37 views
-2

我想要求用户选择一个测验,从txt文件中读取相关问题,请求用户答案,验证并检查它们是正确的,然后加起来得分。我完全自学,所以从各个网站上挑选了这些代码,但是当我调整它时,它不再有效 - 我做错了什么?我知道它可能非常明显,所以请对我温柔!阅读测验文本文件不再工作

获取消息的全局名称the_file没有定义

import time 

def welcome(): 
    print ("Welcome to Mrs Askew's GCSE ICT Quiz") 
    print() 

def get_name(): 
    firstname = input("What is your first name?:") 
    secondname = input("What is your second name?:") 
    print ("Good luck", firstname,", lets begin") 
    return firstname 
    return secondname 

def displaymenu(): 
    print("-------------------------------------------------------") 
    print("Menu") 
    print() 
    print("1. Input and Output Devices") 
    print("2. Collaborative working") 
    print("3. quiz3") 
    print("4. quiz4") 
    print("5. view scores") 
    print("6. Exit") 
    print() 
    print("-------------------------------------------------------") 

def getchoice(): 
    while True: 
     print("enter number 1 to 6") 
     quizchoice = input() 
     print("You have chosen number "+quizchoice) 
     print() 
     if quizchoice >='1' and quizchoice <='6': 
      print("that is a valid entry") 
      break 
     else: 
      print("invalid entry") 
    return quizchoice 

def main(): 
    welcome() 
    get_name() 
    while True: 
     displaymenu() 
     quizchoice = getchoice() 
     print ("please chooses from the options above: ") 
     if quizchoice == ("1"): 
      the_file = open("questions.txt", "r") 
      startquiz() 
     elif quizchoice == ("2"): 
      collaborativeworking() 
      startquiz() 
     else: 
      break 

def collborativeworking(): 
    the_file = open("Collaborative working.txt", "r") 
    return the_file 

def next_line(the_file): 
    line = the_file.readline() 
    line = line.replace("/", "\n") 
    return line 

def next_block(the_file): 
    category = next_line(the_file) 
    question = next_line(the_file) 
    answers = [] 

    for i in range(4): 
     answers.append(next_line(the_file)) 

    correct = next_line(the_file) 
    if correct: 
     correct=correct[0] 

    explanation = next_line(the_file) 
    time.sleep(2) 

    return category, question, answers, correct, explanation  

def startquiz(): 
    title = next_line(the_file) 
    score = 0 
    category, question, answers, correct, explanation = next_block(the_file) 
    while category: 

    # ask a question 
     print(category) 
     print(question) 
     for i in range(4): 
      print("\t", i + 1, "-", answers[i]) 
    # get answer and validate 
     while True: 
      answer =(input("What's your answer?: ")) 
      if answer >= '1' and answer <='4': 
       break 
      else: 
       print ("the number needs to be between 1 and 4, try again ") 

    # check answer 
     answer=str(answer) 
     if answer == correct: 
      print("\nRight!", end=" ") 
    return score 


main() 
+4

你得到什么错误? – Mukul215

+1

这是怎么缩进你的问题运行? – jacoblaw

+2

欢迎来到StackOverflow。请阅读并遵守帮助文档中的发布准则。 [最小,完整,可验证的示例](http://stackoverflow.com/help/mcve)适用于此处。在发布您的MCVE代码并准确描述问题之前,我们无法为您提供有效的帮助。 我们应该能够将发布的代码粘贴到文本文件中,并重现您描述的问题。 – Prune

回答

1

夫妇的事情,我注意到了。您还没有提供的错误你得到(/你有问题),所以这些都是我在快看注意到的事情:

第一:enter code hereimport time应该import time

二:所有函数定义(def func():)应该使其中的代码缩进例如

def get_name(): 
    firstname = input("What is your first name: ") 

三:print()应该print()

四:多行字符串确实存在

""" 
Look at me mum! 
WOWWWW! 
""" 

第五:它看起来像很多这已经从其他地方复制,如果你正在学习,我建议你不要复制,但试着去了解正在做什么事情然后手写它

第六:有很多的错误。我想我得到了他们中的大部分,但是你应该改变你工作的方式。它确实无法在这么多地方

第七:这里是你的代码一些改进:

import time 

def welcome(): 
    print("Welcome to Mrs Askew's GCSE ICT Quiz\n") 

def get_name(): 
    firstname = input("What is your first name: ") 
    secondname = input("What is your second name: ") 
    print("Good luck" + firstname + ", lets begin") # or print("Good luck {}, lets begin".format(firstname)) 
    return firstname, secondname 

def displaymenu(): 
    print("""------------------------------------------------------- 
Menu 
1. Input and Output Devices 
2. Collaborative working 
3. quiz3 
4. quiz4 
5. view scores 
6. Exit 
-------------------------------------------------------""") 

def getchoice(): 
    while True: 
     quizchoice = input("Enter number 1 to 6: ") 
     print("You have chosen number " + quizchoice "\n") 
     if quizchoice >= "1" and quizchoice <= "6": 
      print("That is a valid entry") 
      break 
     else: 
      print("invalid entry") 
    return quizchoice 

def main(): 
    welcome() 
    get_name() 
    while True: 
     displaymenu() 
     quizchoice = getchoice() 
     print("Please chooses from the options above: ") 
     if quizchoice == ("1"): 
      the_file = open("questions.txt", "r") 
      startquiz() 
     elif quizchoice == ("2"): 
      collaborativeworking() 
      startquiz() 
     else: 
      break 

def collborativeworking(): 
    the_file = open("Collaborative working.txt", "r") 
    return the_file 

def next_line(the_file): 
    line = the_file.readline() 
    line = line.replace("/", "\n") 
    return line 

def next_block(the_file): 
    category = next_line(the_file) 
    question = next_line(the_file) 
    answers = [] 

    for i in range(4): 
     answers.append(next_line(the_file)) 

    correct = next_line(the_file) 
    if correct: 
     correct = correct[0] 

    explanation = next_line(the_file) 
    time.sleep(2) 

    return category, question, answers, correct, explanation  

def startquiz(): 
    title = next_line(the_file) 
    score = 0 
    category, question, answers, correct, explanation = next_block(the_file) 
    while category: 

     # ask a question 
     print(category) 
     print(question) 
     for i in range(4): 
      print("\t", i + 1, "-", answers[i]) 
     # get answer and validate 
     while True: 
      answer =(input("What's your answer?: ")) 
      if answer >= '1' and answer <='4': 
        break 
      else: 
       print ("the number needs to be between 1 and 4, try again ") 

     # check answer 
     answer=str(answer) 
     if answer == correct: 
      print("\nRight!", end=" ") 
    return score 


main() 

第八名:请花时间,在提问之前作出答复。这里的人们想回答问题,并试图用尽可能多的努力来回答问题,所以我们希望提问者投入相同的努力,把时间花在他们的问题上(基本上假装你问的是你最尊重的人)你的生活这个问题,然后适当地表现出来,例如,亲爱的贵族殿下,亲爱的主人和恩人,我非常喜欢的人,拜托,拜托,请大家好心地回答我谦卑的问题,我花了2天时间写得这么好你不会被冒犯,并且尽可能少地浪费时间与我这样的小事情...)

第九:有很多更好的方法来做你想做的事情。

我建议你:

+0

你能解释一下函数声明应该放在函数调用之上吗? – Jaskew

+0

@Jaskew收回,见https://stackoverflow.com/questions/3754240/declare-function-at-end-of-file-in-python – ratskin

+0

@Jaskew嗨,如果这或任何回答已经解决了您的问题,请考虑[接受它](https://meta.stackexchange.com/q/5234/179419)通过点击复选标记。这向更广泛的社区表明,您已经找到了解决方案,并为答复者和您自己提供了一些声誉。没有义务这样做。 _Also [这里](https://stackoverflow.com/a/44306759/7304372)_ – ratskin