阅读测验文本文件不再有效



我试图让用户选择一个测验,从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()

我注意到了几件事。您尚未提供您遇到的错误(/您遇到的问题),因此这些是我在快速浏览中注意到的事情:

第一: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 Quizn")
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 天时间写好,这样你就不会被冒犯,不得不浪费尽可能少的时间在我这样的小事上......

第九:有更好的方法可以做你想做的事。

我建议你:

  • 在编写 108 行之前尝试完善程序的某些部分
  • 尽量不要有那么多功能
  • 尝试减少代码的长度,使其不那么冗长,但
  • 简洁
  • 使用一致的格式(例如,仅一种类型的引号;仅空格或制表符)
  • 还要阅读一些基本的python(学习python很难是好的)
  • 我也强烈推荐PEP(Python Enhancement Proposals)8和其他PEP。
  • 看看 python 中的测试驱动开发

最新更新