Python 阅读测验文本文件不再有效
我试图让用户选择一个测验,从txt文件中阅读相关问题,询问用户答案,验证并检查它们是否正确,然后将分数相加。我完全是自学成才的,所以我从不同的网站上学习了这段代码,但随着我的修改,它不再工作了——我做错了什么?我知道这可能是很明显的,所以请对我温柔一点 获取未定义的\u文件的消息全局名称Python 阅读测验文本文件不再有效,python,Python,我试图让用户选择一个测验,从txt文件中阅读相关问题,询问用户答案,验证并检查它们是否正确,然后将分数相加。我完全是自学成才的,所以我从不同的网站上学习了这段代码,但随着我的修改,它不再工作了——我做错了什么?我知道这可能是很明显的,所以请对我温柔一点 获取未定义的\u文件的消息全局名称 import time def welcome(): print ("Welcome to Mrs Askew's GCSE ICT Quiz") print() def get_name(
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()
导入时间
def welcome():
打印(“欢迎参加Askew夫人的GCSE ICT测验”)
打印()
def get_name():
firstname=输入(“您的名字是什么?:”)
secondname=输入(“您的第二个名字是什么?:”)
打印(“祝你好运”,名字,“让我们开始”)
返回名字
返回第二个名称
def displaymenu():
打印(“--------------------------------------------------------------”)
打印(“菜单”)
打印()
打印(“1.输入和输出设备”)
打印(“2.协作工作”)
打印(“3.quiz3”)
打印(“4.quiz4”)
打印(“5.查看分数”)
打印(“6.退出”)
打印()
打印(“--------------------------------------------------------------”)
def getchoice():
尽管如此:
打印(“输入数字1到6”)
quizchoice=input()
打印(“您已选择数字”+quizchoice)
打印()
如果quizchoice>='1'和quizchoice='1'并回答我注意到的两件事。您没有提供您所遇到的错误(/您遇到的问题),因此我在快速查看中注意到以下几点:
首先:在此输入代码导入时间应为导入时间
Second:所有函数定义(def func():
)中的代码都应该缩进,例如
def get_name():
firstname = input("What is your first name: ")
第三:print()
应该是print()
Fourth:多行字符串确实存在
"""
Look at me mum!
WOWWWW!
"""
Fifth:看起来这其中很多都是从其他地方抄袭的,如果你正在学习,我建议你不要抄袭,而是试着理解正在做的事情,然后手写出来
第六:有很多bug。我想我得到了大部分,但你真的应该改变你工作的方式。它确实在很多地方失败了
第七点:以下是您的代码,其中有一些改进:
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()
导入时间
def welcome():
打印(“欢迎参加Askew夫人的GCSE ICT测验\n”)
def get_name():
firstname=输入(“您的名字是什么:”)
secondname=输入(“您的第二个名字是什么:”)
打印(“好运”+名字+”,让我们开始”)或打印(“好运{},让我们开始”。格式(名字))
返回firstname,secondname
def displaymenu():
打印(“”)-------------------------------------------------------
菜单
1.输入和输出设备
2.协作工作
3.quiz3
4.quiz4
5.查看分数
6.退出
-------------------------------------------------------""")
def getchoice():
尽管如此:
quizchoice=input(“输入数字1到6:”)
打印(“您已选择数字”+quizchoice”\n)
如果quizchoice>=“1”和quizchoice=“1”,并回答您得到的错误是什么?这是如何缩进您运行的问题?欢迎使用StackOverflow。请阅读并遵循帮助文档中的发布指南。适用于这里。在您发布MCVE代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中,并重现您描述的问题。一个问题是您使用的变量超出了它们的范围。我强烈建议您练习增量编程。写几行,调试它们,在修复之前不要再添加更多。发布的代码有太多问题,无法在一个问题中解决。在你意识到你有问题之前,你是如何得到超过100行代码的?你能解释一下你的函数声明是否应该放在函数调用之上吗?“Jaskw缩回,见Hi @ Jaskew。如果这个或任何答案已经解决了你的问题,请点击检查标记来考虑。这向更广泛的社区表明,你已经找到了一个解决方案,并给回答者和你自己带来了一些声誉。没有义务这样做。阿尔索